209. Minimum Size Subarray Sum [

2019-06-18  本文已影响0人  一个想当大佬的菜鸡

209. Minimum Size Subarray Sum

209. Minimum Size Subarray Sum

左右两个指针维护一个窗口,停止条件是右边指针小于数组长度,当前窗口和大于s时,左边指针加一,直到小于s,再移动右边指针。

class Solution(object):
    def minSubArrayLen(self, s, nums):
        """
        :type s: int
        :type nums: List[int]
        :rtype: int
        """
        res = float('inf')
        l = r = curSum = 0
        while r < len(nums):
            curSum += nums[r]
            while curSum >= s:
                res = min(res, r - l + 1)
                curSum -= nums[l]
                l += 1
            r += 1
        return res if res!=float('inf') else 0
上一篇下一篇

猜你喜欢

热点阅读