Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 605 Bytes

376.md

File metadata and controls

28 lines (25 loc) · 605 Bytes

376. 摆动序列

贪心

每次考虑右边的点,初始为1个或者2个。

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int size = nums.size();
        if (size < 2) {
            return 1;
        }
        int ans = 0;
        int pre = nums[1] - nums[0];
        ans = pre != 0 ? 2 : 1;
        for (int i = 2; i < nums.size(); i++) {
            int cur = nums[i] - nums[i-1];
            if ((cur > 0 && pre <= 0) || (cur < 0 && pre >= 0)) {
                ans++;
                pre = cur;
            }
        }
        return ans;
    }
};