# 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 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up