贪心算法 | LeetCode 376. 摆动序列

找出局部峰值即可

贪心算法 | LeetCode 376. 摆动序列_第1张图片

    public int wiggleMaxLength(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        int result = 1, front = 0, behind = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            front = nums[i] - nums[i + 1];
            if ((front > 0 && behind < 0) || (front < 0 && behind >= 0)){
                behind = front;
                result++;
            }
        }
        return result;
    }

 

 

你可能感兴趣的:(贪心算法,leetcode,蓝桥杯)