Skip to content

Latest commit

 

History

History
21 lines (19 loc) · 514 Bytes

77. Combinations.md

File metadata and controls

21 lines (19 loc) · 514 Bytes

77. Combinations (Medium)

[tag] DFS, combination

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        nums = [i for i in range(1, n+1)]
        temp = []
        ans = []
        
        def dfs(index):
            if len(temp) == k:
                ans.append(temp.copy())
                return
            for i in range(index, len(nums)):
                temp.append(nums[i])
                dfs(i+1)
                temp.pop()
        dfs(0)
        return ans