반응형
leetcode.com/problems/combinations/
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
nums = [i for i in range(1,n+1)]
result = []
def dfs(lst, unused):
if len(lst)==k:
result.append(lst)
return
for num in unused:
idx = unused.index(num)
dfs(lst+[num], unused[idx+1:])
dfs([], nums)
return result
댓글