Skip to content

Commit 0fc1781

Browse files
Merge pull request youngyangyang04#984 from Reoooh/master
更新0209长度最小的子数组:添加Ruby版本(2.x、3.x)
2 parents 3fdc41e + 4e2c449 commit 0fc1781

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

problems/0209.长度最小的子数组.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public:
107107
};
108108
```
109109

110-
时间复杂度:$O(n)$
110+
时间复杂度:$O(n)$
111111
空间复杂度:$O(1)$
112112

113113
**一些录友会疑惑为什么时间复杂度是$O(n)$**
@@ -121,7 +121,6 @@ public:
121121

122122

123123

124-
125124
## 其他语言版本
126125

127126

@@ -291,5 +290,23 @@ class Solution {
291290
}
292291
```
293292

293+
Ruby:
294+
295+
```ruby
296+
def min_sub_array_len(target, nums)
297+
res = Float::INFINITY # 无穷大
298+
i, sum = 0, 0
299+
nums.length.times do |j|
300+
sum += nums[j]
301+
while sum >= target
302+
res = [res, j - i + 1].min
303+
sum -= nums[i]
304+
i += 1
305+
end
306+
end
307+
res == Float::INFINITY ? 0 : res
308+
end
309+
```
310+
294311
-----------------------
295312
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)