Created
July 28, 2021 08:42
-
-
Save cjnghn/0efa5ea3e124fe1f4e94cecb897a32e3 to your computer and use it in GitHub Desktop.
77. Combinations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def combine(self, n: int, k: int) -> List[List[int]]: | |
def dfs(nums: List[int], path: List[int]): | |
if len(path) == k: | |
res.append(path) | |
return | |
for i in range(len(nums)): | |
dfs( | |
nums[i+1:], | |
path + [nums[i]] | |
) | |
res = [] | |
dfs([i + 1 for i in range(n)], []) | |
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def combine(self, n: int, k: int) -> List[List[int]]: | |
return list(itertools.combinations(range(1, n + 1), k)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment