class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
#因为算一个差值,长度是2,所以res初始为1
res = 1
p_n = None
n = len(nums)
for i in range(1,n):
sub = nums[i]-nums[i-1]
cur = 0 if sub<0 else 1
if sub == 0:
continue
if i == 1:
p_n = cur
#这里res要加1,这里是第一个差值,但算完长度就为2了,所以得+1
res += 1
if cur != p_n:
res += 1
p_n = cur
return res
376. 摆动序列
于 2024-02-08 21:25:15 首次发布