滑动窗口+前缀和-9--LC1248.统计[优美子数组]

滑动窗口+前缀和-9--LC1248.统计[优美子数组]_第1张图片

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)     

你可能感兴趣的:(leetcode,leetcode,滑动窗口,前缀和)