编程语言
首页 > 编程语言> > Leetcode:209. 长度最小的子数组(Python3)

Leetcode:209. 长度最小的子数组(Python3)

作者:互联网

 

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        # 边界条件
        if not nums or len(nums) == 0:
            return 0
        left, right = 0, 0
        total, result = 0, len(nums)+1
        # 滑动
        while right < len(nums):
            total += nums[right]
            right += 1
            while total >= target:
                result = min(result, right-left)
                total -= nums[left]
                left += 1
        # 返回结果
        return result if result != len(nums)+1 else 0

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        # 边界条件
        if not nums or len(nums) == 0:
            return 0
        total, result = 0, len(nums)+1
        for left in range(len(nums)):
            total = 0
            for right in range(left,len(nums)):
                total += nums[right]
                if total >= target:
                    result = min(result, right-left+1)
                    break
        return result if result != len(nums)+1 else 0

标签:right,nums,209,len,result,total,Leetcode,Python3,left
来源: https://blog.csdn.net/weixin_54310596/article/details/122603249