376. 摆动序列
--解题思路--
数组长度:
1.长度<=1时:
返回值:数组长度
2.长度>1时:
up,down交替记录摆动情况(初始值=1)
(1)当num[i]-num[i+1]>0时(下降趋势)
down = up + 1;
(2)当num[i]-num[i+1]<0时(上升趋势)
up = down + 1;
返回值 max(up,down)
--解题代码--
public int wiggleMaxLength(int[] nums) { if (nums.length <= 1) return nums.length; else { int up=1; int down=1; for(int i=0;i<nums.length-1;++i){ if(nums[i]-nums[i+1]<0){ down = up+1; } else if(nums[i]-nums[i+1]>0){ up = down+1; } } return up>down?up:down; } }