Skip to content

Commit 8270c72

Browse files
authored
添加 0077.组合优化 python版本
添加 0077.组合优化 python版本
1 parent ff3605a commit 8270c72

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

problems/0077.组合优化.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,22 @@ class Solution {
176176
```
177177

178178
Python:
179-
180-
179+
```python3
180+
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+
```
181195
Go:
182196

183197

0 commit comments

Comments
 (0)