Preview:
def combine(self, n: int, k: int) -> List[List[int]]:
        res = []
        nums = range(1,n+1);

        def dfs(k, index, path):
            if k == 0:
                res.append(path)
                return # backtracking 
            for i in range(index, len(nums)):
                dfs(k-1, i+1, path+[nums[i]])
               
        dfs(k, 0 ,[])
        return res
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter