class Solution(object):
def numberOfSubarrays(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
def leK(nums, k):
start = 0
odd_count = 0
count = 0
for end in range(len(nums)):
if nums[end] % 2 == 1:
odd_count += 1
while odd_count > k:
if nums[start] % 2 == 1:
odd_count -= 1
start += 1
count += end-start+1
return count
return leK(nums, k) - leK(nums, k-1)
滑动窗口+前缀和-9--LC1248.统计[优美子数组]
最新推荐文章于 2024-05-01 16:18:57 发布