mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0209.长度最小的子数组.md
滑动窗口 version of python3 code
This commit is contained in:
@ -162,8 +162,27 @@ class Solution:
|
|||||||
index += 1
|
index += 1
|
||||||
return 0 if res==float("inf") else res
|
return 0 if res==float("inf") else res
|
||||||
```
|
```
|
||||||
|
```python3
|
||||||
|
#滑动窗口
|
||||||
|
class Solution:
|
||||||
|
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
|
||||||
|
if nums is None or len(nums)==0:
|
||||||
|
return 0
|
||||||
|
lenf=len(nums)+1
|
||||||
|
total=0
|
||||||
|
i=j=0
|
||||||
|
while (j<len(nums)):
|
||||||
|
total=total+nums[j]
|
||||||
|
j+=1
|
||||||
|
while (total>=target):
|
||||||
|
lenf=min(lenf,j-i)
|
||||||
|
total=total-nums[i]
|
||||||
|
i+=1
|
||||||
|
if lenf==len(nums)+1:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return lenf
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
func minSubArrayLen(target int, nums []int) int {
|
func minSubArrayLen(target int, nums []int) int {
|
||||||
|
Reference in New Issue
Block a user