2760.最长奇偶子数组
class Solution(object):
def longestAlternatingSubarray(self, nums, threshold):
"""
:type nums: List[int]
:type threshold: int
:rtype: int
"""
# 数组长度
n=len(nums)
# 返回结果,和数组下标
ans=i=0
while i<n:
# nums[l] % 2 == 0
# 对于范围 [l, r] 内的所有下标 i ,nums[i] <= threshol
if nums[i]>threshold or nums[i]%2==1:
i+=1
# 不符合跳过
continue
l=i
i+=1
while i<n and nums[i]<=threshold and nums[i]%2!=nums[i-1]%2:
i+=1
ans =max(ans,i-l)
return ans
class Solution(object):
def longestAlternatingSubarray(self, nums, threshold):
"""
:type nums: List[int]
:type threshold: int
:rtype: int
"""
# 数组长度
n=len(nums)
# 返回结果,和数组下标
ans=i=0
while i<n:
# nums[l] % 2 == 0
# 对于范围 [l, r] 内的所有下标 i ,nums[i] <= threshol
if nums[i]>threshold or nums[i]%2==1:
i+=1
# 不符合跳过
continue
l=i
i+=1
while i<n and nums[i]<=threshold and nums[i]%2!=nums[i-1]%2:
i+=1
ans =max(ans,i-l)
return ans