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

This commit is contained in:
YDLIN
2021-08-09 14:20:32 +08:00
parent 369e55ed67
commit 1d64c61373

View File

@ -216,6 +216,29 @@ var minSubArrayLen = function(target, nums) {
};
```
Swift:
```swift
func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
var result = Int.max
var sum = 0
var starIndex = 0
for endIndex in 0..<nums.count {
sum += nums[endIndex]
while sum >= target {
result = min(result, endIndex - starIndex + 1)
sum -= nums[starIndex]
starIndex += 1
}
}
return result == Int.max ? 0 : result
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)