376. Wiggle Subsequence

2018-07-26  本文已影响0人  becauseyou_90cd

https://leetcode.com/problems/wiggle-subsequence/description/
subsequence代表数字间隔in array
解题思路:up and down represents greater and lesser.

代码:
class Solution {
public int wiggleMaxLength(int[] nums) {

    if(nums.length == 0) return 0;
    int up = 1;
    int down = 1;
    for(int i = 1; i < nums.length; i++){
        if(nums[i] > nums[i - 1]){
            up = down + 1;
        }else if(nums[i] < nums[i - 1]){
            down = up + 1;
        }
    }
    return Math.max(up, down);
}

}

上一篇 下一篇

猜你喜欢

热点阅读