添加 0209.长度最小的子数组 go 版本

This commit is contained in:
NevS
2021-05-20 23:51:45 +08:00
committed by GitHub
parent 942bd655b6
commit 467461c24e

View File

@ -172,6 +172,30 @@ Python
Go 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: JavaScript: