mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #984 from Reoooh/master
更新0209长度最小的子数组:添加Ruby版本(2.x、3.x)
This commit is contained in:
@ -107,7 +107,7 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
时间复杂度:$O(n)$
|
时间复杂度:$O(n)$
|
||||||
空间复杂度:$O(1)$
|
空间复杂度:$O(1)$
|
||||||
|
|
||||||
**一些录友会疑惑为什么时间复杂度是$O(n)$**。
|
**一些录友会疑惑为什么时间复杂度是$O(n)$**。
|
||||||
@ -121,7 +121,6 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
@ -291,5 +290,23 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Ruby:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
def min_sub_array_len(target, nums)
|
||||||
|
res = Float::INFINITY # 无穷大
|
||||||
|
i, sum = 0, 0
|
||||||
|
nums.length.times do |j|
|
||||||
|
sum += nums[j]
|
||||||
|
while sum >= target
|
||||||
|
res = [res, j - i + 1].min
|
||||||
|
sum -= nums[i]
|
||||||
|
i += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
res == Float::INFINITY ? 0 : res
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user