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