mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
@ -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"/>
|
||||
|
Reference in New Issue
Block a user