mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加0077.组合 Ruby实现
This commit is contained in:
@ -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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user