diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 42687514..3a593f6e 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -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..= 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)