mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
@ -172,6 +172,30 @@ Python:
|
||||
|
||||
|
||||
Go:
|
||||
```go
|
||||
func minSubArrayLen(target int, nums []int) int {
|
||||
i := 0
|
||||
l := len(nums) // 数组长度
|
||||
sum := 0 // 子数组之和
|
||||
result := l + 1 // 初始化返回长度为l+1,目的是为了判断“不存在符合条件的子数组,返回0”的情况
|
||||
for j := 0; j < l; j++ {
|
||||
sum += nums[j]
|
||||
for sum >= target {
|
||||
subLength := j - i + 1
|
||||
if subLength < result {
|
||||
result = subLength
|
||||
}
|
||||
sum -= nums[i]
|
||||
i++
|
||||
}
|
||||
}
|
||||
if result == l+1 {
|
||||
return 0
|
||||
} else {
|
||||
return result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
JavaScript:
|
||||
|
Reference in New Issue
Block a user