Update 0077.组合优化.md

This commit is contained in:
jianghongcheng
2023-05-26 02:25:21 -05:00
committed by GitHub
parent 89e9d75cbf
commit 4d48c45107

View File

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