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
网友评论