修改 0077.组合 python3版本

修改 0077.组合 python3版本
This commit is contained in:
jojoo15
2021-05-23 21:31:51 +02:00
committed by GitHub
parent ff3605a52b
commit ea92a02dd0

View File

@ -370,23 +370,21 @@ class Solution {
Python
```python
```python3
class Solution:
result: List[List[int]] = []
path: List[int] = []
def combine(self, n: int, k: int) -> List[List[int]]:
self.result = []
self.combineHelper(n, k, 1)
return self.result
def combineHelper(self, n: int, k: int, startIndex: int):
if (l := len(self.path)) == k:
self.result.append(self.path.copy())
return
for i in range(startIndex, n - (k - l) + 2):
self.path.append(i)
self.combineHelper(n, k, i + 1)
self.path.pop()
res=[] #存放符合条件结果的集合
path=[] #用来存放符合条件结果
def backtrack(n,k,startIndex):
if len(path) == k:
res.append(path[:])
return
for i in range(startIndex,n+1):
path.append(i) #处理节点
backtrack(n,k,i+1) #递归
path.pop() #回溯,撤销处理的节点
backtrack(n,k,1)
return res
```
javascript
```javascript