分析:
这题是找满足和大于等于target的最短数组
有点小问题,想用双指针做,但是写得有点糅杂了
最后一组案例时间超了
最后借鉴了一下题解写出来
代码:
1 class Solution(object): 2 def minSubArrayLen(self, target, nums): 3 """ 4 :type target: int 5 :type nums: List[int] 6 :rtype: int 7 """ 8 # count=sum(nums[0:-1])+nums[-1] 9 # if count<target: 10 # return 0 11 # elif count==target: 12 # return len(nums) 13 14 # mins=1e9 15 # for i in range(len(nums)): 16 # count=0 17 # # left=i 18 # right=i 19 # mine=0 20 # while right!=len(nums): 21 # count=count+nums[right] 22 # mine+=1 23 # if count>=target: 24 # mins=min(mine,mins) 25 # break 26 # else: 27 # right+=1 28 # return mins 29 n=len(nums) 30 left=0 31 answer=n+1 32 sum_nums=0 33 for right,x in enumerate(nums): 34 sum_nums+=x 35 while sum_nums>=target: 36 answer=min(answer,right-left+1) 37 sum_nums-=nums[left] 38 left+=1 39 return answer if answer<=n else 0
标签:right,target,nums,209,sum,数组,answer,长度,left From: https://www.cnblogs.com/ooooopppp/p/17375106.html