当前位置:网站首页>Leetcode-209 minimum length subarray

Leetcode-209 minimum length subarray

2022-06-11 00:03:00 It was the sea

 Insert picture description here

class Solution(object):
    def minSubArrayLen(self, target, nums):
        """
        :type target: int
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        
        n = len(nums)
        ans = n + 1
        start, end = 0, 0
        total = 0
        while end < n:
            total += nums[end]
            while total >= target:
                ans = min(ans, end - start + 1)
                total -= nums[start]
                start += 1
            end += 1
        
        return 0 if ans == n + 1 else ans
原网站

版权声明
本文为[It was the sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020932441319.html