回溯--组合型
例题:
方法一:枚举下一个数选哪个
class Solution: def combine(self, n: int, k: int) -> List[List[int]]: ans = [] path = [] def dfs(i: int) -> None: d = k - len(path) # 还要选 d 个数 if d == 0: # 选好了 ans.append(path.copy()) return for j in range(i, d - 1, -1): path.append(j) dfs(j - 1) path.pop() # 恢复现场 dfs(n) return ans
方法二:选或不选
class Solution: def combine(self, n: int, k: int) -> List[List[int]]: ans = [] path = [] def dfs(i: int) -> None: d = k - len(path) # 还要选 d 个数 if d == 0: # 选好了 ans.append(path.copy()) return # 如果 i > d,可以不选 i if i > d: dfs(i - 1) # 选 i path.append(i) dfs(i - 1) path.pop() # 恢复现场 dfs(n) return ans
class Solution: def generateParenthesis(self, n: int) -> List[str]: m = n*2 ans = [] path = [''] * m def dfs(i, open): if i == m: ans.append(''.join(path)) return if open < n: path[i] = '(' dfs(i+1, open+1) if i-open < open: path[i] = ')' dfs(i+1, open) dfs(0,0) return ans