Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e207bf

Browse files
添加0077.组合 Ruby实现
1 parent 5df6ab7 commit 1e207bf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎problems/0077.组合.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,35 @@ object Solution {
764764
}
765765
```
766766

767+
### Ruby
768+
769+
```ruby
770+
771+
def combine(n, k)
772+
result = []
773+
path = []
774+
backtracking(result, path, n, 1, k)
775+
return result
776+
end
777+
778+
#剪枝优化
779+
def backtracking(result, path, n, j, k)
780+
if path.size == k
781+
result << path.map {|item| item}
782+
return
783+
end
784+
785+
for i in j..(n-(k - path.size)) + 1
786+
#处理节点
787+
path << i
788+
backtracking(result, path, n, i + 1, k)
789+
#回溯,撤销处理过的节点
790+
path.pop
791+
end
792+
end
793+
794+
```
795+
767796
<p align="center">
768797
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
769798
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /