mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -269,18 +269,23 @@ class Solution:
|
|||||||
**贪心**
|
**贪心**
|
||||||
```golang
|
```golang
|
||||||
func wiggleMaxLength(nums []int) int {
|
func wiggleMaxLength(nums []int) int {
|
||||||
var count, preDiff, curDiff int //初始化默认为0
|
n := len(nums)
|
||||||
count = 1 // 初始化为1,因为最小的序列是1个数
|
if n < 2 {
|
||||||
if len(nums) < 2 {
|
return n
|
||||||
return count
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(nums)-1; i++ {
|
ans := 1
|
||||||
curDiff = nums[i+1] - nums[i]
|
prevDiff := nums[1] - nums[0]
|
||||||
if (curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0) {
|
if prevDiff != 0 {
|
||||||
count++
|
ans = 2
|
||||||
|
}
|
||||||
|
for i := 2; i < n; i++ {
|
||||||
|
diff := nums[i] - nums[i-1]
|
||||||
|
if diff > 0 && prevDiff <= 0 || diff < 0 && prevDiff >= 0 {
|
||||||
|
ans++
|
||||||
|
prevDiff = diff
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return count
|
return ans
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user