题目
题目分析
QQ图片20200103222435.jpg代码
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if (nums.empty()){
return 0;
}
int cnt1 = 1, cnt2 = 1;
int num1 = nums[0], num2 = nums[0];
for (int i = 1; i < nums.size(); i++){
int t_cnt1 = cnt1, t_num1 = num1;
int t_cnt2 = cnt2, t_num2 = num2;
if (nums[i] > t_num1){
cnt2 = t_cnt1 + 1;
num2 = nums[i];
}
if (nums[i] < t_num2){
cnt1 = t_cnt2 + 1;
num1 = nums[i];
}
}
return max(cnt1, cnt2);
}
};
网友评论