题目:
代码(首刷看解析 2024年2月9日):
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int direction = 0;//1上升,0下降
int res = 0;//res记录曲线变化的次数
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1]) continue;
else if (nums[i] > nums[i - 1]) {
if (direction == 1) continue;
direction = 1;
res++;
} else {
if (direction == -1) continue;
direction = -1;
res++;
}
}
return res + 1;
}
};