# Leetcode 77. Combinations ## 題解 ## DFS Back Tracking ```python=! class Solution: def combine(self, n: int, k: int) -> List[List[int]]: output = [] def dfs(depth: int,start: int,ans: List[int]): if depth == k: output.append(ans[:]) else: for i in range(start,n+1): # 每一層都要把自己排除,才不會有重複解 ans.append(i) dfs(depth+1,i+1,ans) ans.pop() dfs(0,1,[]) return output ```