数组_leetcode209

#coding=utf-8
# 解题思路:滑动窗口 (本质上是搜索剪支) 20190302 找工作期间


class Solution(object):
def minSubArrayLen(self, s, nums):
"""
:type s: int
:type nums: List[int]
:rtype: int
"""
l = 0
r = -1
sum = 0
res = len(nums)+1

while(l < len(nums)):
if r+1 < len(nums) and sum < s :
sum += nums[r+1]
r += 1

else:
if r+1 == len(nums) and sum <s :
sum -= nums[l]
l += 1



if r+1 == len(nums) and sum >= s:
res = min(res,r-l+1)
sum -= nums[l]
l += 1
if r+1 < len(nums) and sum >= s:
res = min(res, r - l + 1)
sum -= nums[l]
l += 1







if res == len(nums)+1:
return 0
else :
return res


t = 7
nums = [2,3,1,2,4,3]

s = Solution()
print s.minSubArrayLen(7,nums)
posted @ 2019-03-17 14:24  AceKo  阅读(91)  评论(0编辑  收藏  举报