Last active
August 26, 2020 03:56
-
-
Save nma/032c71ee6ccf68375cac8611aa1c105c to your computer and use it in GitHub Desktop.
Combinations Recursive
This file contains hidden or 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]]: | |
nums = list(range(1, n + 1)) | |
combinations = [] | |
def combine_helper(ans: List[int], path: List[int], options: List[int], count: int): | |
if count == 0: | |
ans.append(path) | |
else: | |
for index, num in enumerate(options): | |
combine_helper(ans, path+[num], options[index + 1:], count - 1) | |
combine_helper(combinations, [], nums, k) | |
return combinations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment