We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ff3605a commit 8270c72Copy full SHA for 8270c72
problems/0077.组合优化.md
@@ -176,8 +176,22 @@ class Solution {
176
```
177
178
Python:
179
-
180
+```python3
+class Solution:
181
+ def combine(self, n: int, k: int) -> List[List[int]]:
182
+ res=[] #存放符合条件结果的集合
183
+ path=[] #用来存放符合条件结果
184
+ def backtrack(n,k,startIndex):
185
+ if len(path) == k:
186
+ res.append(path[:])
187
+ return
188
+ for i in range(startIndex,n-(k-len(path))+2): #优化的地方
189
+ path.append(i) #处理节点
190
+ backtrack(n,k,i+1) #递归
191
+ path.pop() #回溯,撤销处理的节点
192
+ backtrack(n,k,1)
193
+ return res
194
+```
195
Go:
196
197
0 commit comments