基础算法:滑动窗口_python版本
能使用滑动窗口的题,基本都需要数字为正整数,这样才能保证滑入一个数字总和是增加的(单调性)
一、209. 长度最小的子数组

- 思路:
已每个位置为右端点,依次加大左端点,最短不满足 sum(num[left,right]) < target的。 - 代码:
classSolution:defminSubArrayLen(self, target:int, nums: List[int])->int: n =len(nums) ans = n +1# 也可以写 inf s = left =0for right, x inenumerate(nums):# 枚举子数组右端点 s += x while s >= target: