其他分享
首页 > 其他分享> > leetcode每日一题【Day3】——209. 长度最小的子数组

leetcode每日一题【Day3】——209. 长度最小的子数组

作者:互联网

#直接用end-start+1记录长度
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
    left=0
    sum=0
    min_length=math.inf
    for right in range(len(nums)):
        sum+=nums[right]
        while sum>=target:
            min_length=min(min_length,right-left+1)
            sum-=nums[left]
            left+=1   
    return 0 if min_length==math.inf else min_length
    
#用length记录长度
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
    left=0
    sum=0
    length=0
    min_length=math.inf
    for right in range(len(nums)):
        sum+=nums[right]
        length+=1
        while sum>=target:
            min_length=min(min_length,length)
            sum-=nums[left]
            left+=1    
            length-=1
    return 0 if min_length==math.inf else min_length

明日计划:1695. 删除子数组的最大得分

标签:right,min,209,sum,nums,Day3,length,leetcode,left
来源: https://blog.csdn.net/weixin_45366750/article/details/119773863