class Solution {
public int wiggleMaxLength(int[] nums) {
if(nums.length <= 1){
return nums.length;
}
// 上一个差值
int preDiff = 0;
// 当前差值
int curDiff = 0;
// 默认有一个峰值
int diffCount = 1;
for(int i=1; i<nums.length; i++){
curDiff = nums[i] - nums[i-1];
if((curDiff < 0 && preDiff >= 0) || (curDiff > 0 && preDiff <=0)){
preDiff = curDiff;
diffCount++;
}
}
return diffCount;
}
}
力扣376题 摆动序列 Java版
最新推荐文章于 2024-09-05 22:39:50 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)