添加0077.组合 Ruby实现

This commit is contained in:
han
2023-08-24 20:47:01 +08:00
parent 5df6ab7bdf
commit 1e207bfc6b

View File

@ -764,6 +764,35 @@ object Solution {
}
```
### Ruby
```ruby
def combine(n, k)
result = []
path = []
backtracking(result, path, n, 1, k)
return result
end
#剪枝优化
def backtracking(result, path, n, j, k)
if path.size == k
result << path.map {|item| item}
return
end
for i in j..(n-(k - path.size)) + 1
#处理节点
path << i
backtracking(result, path, n, i + 1, k)
#回溯,撤销处理过的节点
path.pop
end
end
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>