文章目录
- 376. 摆动序列
376. 摆动序列
题目链接:https://leetcode.cn/problems/wiggle-subsequence/description/
这个题目自己首先想到的是动态规划解题,贪心解法真的非常妙,参考下面题解:https://leetcode.cn/problems/wiggle-subsequence/solutions/284327/tan-xin-si-lu-qing-xi-er-zheng-que-de-ti-jie-by-lg/
代码:
class Solution {public int wiggleMaxLength(int[] nums) {int len = nums.length;int up = 1, down = 1;for(int i = 1; i < len; i++){if(nums[i] > nums[i - 1]){up = down + 1;}else if(nums[i] < nums[i - 1]){down = up + 1;}}return len == 0 ? 0 : Math.max(up, down);}
}