为数组篇 增加 Swift 实现

This commit is contained in:
zhenzi
2021-07-17 12:13:15 +08:00
parent 2c4afffbd3
commit c14c2297f1
14 changed files with 702 additions and 1 deletions

View File

@@ -106,3 +106,22 @@ class Solution:
return windowlen
```
Swift Code
```swift
class Solution {
func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
var sum = 0, windowlen = Int.max, i = 0
for j in 0..<nums.count {
sum += nums[j]
while sum >= target {
windowlen = min(windowlen, j - i + 1)
sum -= nums[i]
i += 1
}
}
return windowlen == Int.max ? 0 : windowlen
}
}
```