mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
增加 0376.摆动序列 go版本
增加 0376.摆动序列 go版本
This commit is contained in:
@ -151,7 +151,24 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```golang
|
||||||
|
func wiggleMaxLength(nums []int) int {
|
||||||
|
var count,preDiff,curDiff int
|
||||||
|
count=1
|
||||||
|
if len(nums)<2{
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
for i:=0;i<len(nums)-1;i++{
|
||||||
|
curDiff=nums[i+1]-nums[i]
|
||||||
|
//如果有正有负则更新下标值||或者只有前一个元素为0(针对两个不等元素的序列也视作摆动序列,且摆动长度为2)
|
||||||
|
if (curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0){
|
||||||
|
preDiff=curDiff
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Javascript:
|
Javascript:
|
||||||
```Javascript
|
```Javascript
|
||||||
|
Reference in New Issue
Block a user