Leetcode|中等|序列|376. 摆动序列

2021-09-18 16:35:37 浏览数 (1)

1 贪心算法

代码语言:javascript复制
class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int preDiff = 0, curDiff = 0;
        int count = 0;
        for (int i = 0; i < nums.size(); i  ) {
            if (i == 0) {
                count  ;
                continue;
            }
            curDiff = nums[i] - nums[i-1];
            if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) {
                count  ;
                preDiff = curDiff;
            }
        }
        return count;
    }
};

2 动态规划

等二刷时再更新啦~

致谢

图片来源于「代码随想录」公众号,欢迎大家关注这位大佬的公号

0 人点赞