Merge pull request #347 from jojoo15/patch-34

优化 0376. 摆动序列 python3版本
This commit is contained in:
程序员Carl
2021-06-08 15:17:15 +08:00
committed by GitHub

View File

@ -138,20 +138,16 @@ class Solution {
```
Python
```python
```python3
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
#贪心 求波峰数量 + 波谷数量
if len(nums)<=1:
return len(nums)
cur, pre = 0,0 #当前一对差值,前一对差值
count = 1#默认最右边有一个峰值
for i in range(len(nums)-1):
cur = nums[i+1] - nums[i]
if((cur>0 and pre<=0) or (cur<0 and pre>=0)):
count += 1
pre = cur
return count
preC,curC,res = 0,0,1 #题目里nums长度大于等于1当长度为1时其实到不了for循环里去所以不用考虑nums长度
for i in range(len(nums) - 1):
curC = nums[i + 1] - nums[i]
if curC * preC <= 0 and curC !=0: #差值为0时不算摆动
res += 1
preC = curC #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值
return res
```
Go