Skip to content

Commit dd68fc3

Browse files
Merge pull request #237 from jojoo15/patch-20
修改 0077.组合 python3版本
2 parents 13f7aab + ea92a02 commit dd68fc3

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

problems/0077.组合.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -370,23 +370,21 @@ class Solution {
370370

371371

372372
Python:
373-
```python
373+
```python3
374374
class Solution:
375-
result: List[List[int]] = []
376-
path: List[int] = []
377375
def combine(self, n: int, k: int) -> List[List[int]]:
378-
self.result = []
379-
self.combineHelper(n, k, 1)
380-
return self.result
381-
382-
def combineHelper(self, n: int, k: int, startIndex: int):
383-
if (l := len(self.path)) == k:
384-
self.result.append(self.path.copy())
385-
return
386-
for i in range(startIndex, n - (k - l) + 2):
387-
self.path.append(i)
388-
self.combineHelper(n, k, i + 1)
389-
self.path.pop()
376+
res=[] #存放符合条件结果的集合
377+
path=[] #用来存放符合条件结果
378+
def backtrack(n,k,startIndex):
379+
if len(path) == k:
380+
res.append(path[:])
381+
return
382+
for i in range(startIndex,n+1):
383+
path.append(i) #处理节点
384+
backtrack(n,k,i+1) #递归
385+
path.pop() #回溯,撤销处理的节点
386+
backtrack(n,k,1)
387+
return res
390388
```
391389
javascript
392390
```javascript

0 commit comments

Comments
 (0)