Skip to content

Instantly share code, notes, and snippets.

@cjnghn
Created July 28, 2021 08:42
Show Gist options
  • Save cjnghn/0efa5ea3e124fe1f4e94cecb897a32e3 to your computer and use it in GitHub Desktop.
Save cjnghn/0efa5ea3e124fe1f4e94cecb897a32e3 to your computer and use it in GitHub Desktop.
77. Combinations
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
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