mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加0209.长度最小的子数组 Swfit版本
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user