mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加0376.摆动序列 Java版本
This commit is contained in:
@ -111,7 +111,31 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public int wiggleMaxLength(int[] nums) {
|
||||||
|
if (nums == null || nums.length <= 1) {
|
||||||
|
return nums.length;
|
||||||
|
}
|
||||||
|
//当前差值
|
||||||
|
int curDiff = 0;
|
||||||
|
//上一个差值
|
||||||
|
int preDiff = 0;
|
||||||
|
int count = 1;
|
||||||
|
for (int i = 1; i < nums.length; i++) {
|
||||||
|
//得到当前差值
|
||||||
|
curDiff = nums[i] - nums[i - 1];
|
||||||
|
//如果当前差值和上一个差值为一正一负
|
||||||
|
//等于0的情况表示初始时的preDiff
|
||||||
|
if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) {
|
||||||
|
count++;
|
||||||
|
preDiff = curDiff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user