Minimum Size Subarray Sum
要点:O(n)解的基本方法是sliding window,loop到第i个元素表示的值是结尾在i的元素最长序列。所以进入loop首先加上该元素。然后从左边pop
错误点:左边pop是一个while loop而不是仅pop一个元素:可能有一堆小元素进入window,当前元素很大,所以所有之前的小元素都pop也能满足条件
class Solution(object):
def minSubArrayLen(self, s, nums):
"""
:type s: int
:type nums: List[int]
:rtype: int
"""
start = 0
sum = 0
minLen = sys.maxint
for i in range(len(nums)):
sum+=nums[i]
while sum>=s:
if i-start+1<minLen:
minLen = i-start+1
sum-=nums[start]
start+=1
if minLen==sys.maxint: minLen=0
return minLen