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

上一篇:Python微信订餐小程序课程汇总


下一篇:mnist手写数字识别