跳转至

摆动序列⚓︎

Leetcode题目链接

描述⚓︎

详见中文题目链接

解答⚓︎

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int n = nums.size();
        if (n == 1) return n;

        int res = 0;
        string direction{};
        for (int i = 1; i < n; i++) {
            if (nums[i] > nums[i - 1]) {
                if (direction == "up") continue;
                direction = "up";
                res += 1;
            } else if (nums[i] < nums[i - 1]) {
                if (direction == "down") continue;
                direction = "down";
                res += 1;
            }
        }
        return res + 1;
    }
};