mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
优化 0376. 摆动序列 python3版本
优化 0376. 摆动序列 python3版本
This commit is contained in:
@ -138,20 +138,16 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
def wiggleMaxLength(self, nums: List[int]) -> int:
|
def wiggleMaxLength(self, nums: List[int]) -> int:
|
||||||
#贪心 求波峰数量 + 波谷数量
|
preC,curC,res = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度
|
||||||
if len(nums)<=1:
|
for i in range(len(nums) - 1):
|
||||||
return len(nums)
|
curC = nums[i + 1] - nums[i]
|
||||||
cur, pre = 0,0 #当前一对差值,前一对差值
|
if curC * preC <= 0 and curC !=0: #差值为0时,不算摆动
|
||||||
count = 1#默认最右边有一个峰值
|
res += 1
|
||||||
for i in range(len(nums)-1):
|
preC = curC #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值
|
||||||
cur = nums[i+1] - nums[i]
|
return res
|
||||||
if((cur>0 and pre<=0) or (cur<0 and pre>=0)):
|
|
||||||
count += 1
|
|
||||||
pre = cur
|
|
||||||
return count
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user