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);
}
}
网友评论