本题要考虑三种情况:
- 上下坡中有平坡
- 数组首尾两端
- 单调坡中有平坡
class Solution {
public int wiggleMaxLength(int[] nums) {
if(nums.length <= 1) return nums.length;
int curdiff = 0;
int prediff = 0;
int result = 1;
for(int i = 1; i< nums.length;i++){
curdiff = nums[i] - nums[i - 1];
if((curdiff > 0 && prediff <= 0) || (curdiff < 0 && prediff >= 0)){
result++;
prediff = curdiff;
}
}
return result;
}
}