Easy to understand Python solution (backtracking).


  • 8
    C
    def combinationSum3(self, k, n):
        res = []
        self.dfs(xrange(1,10), k, n, 0, [], res)
        return res
        
    def dfs(self, nums, k, n, index, path, res):
        if k < 0 or n < 0: # backtracking 
            return 
        if k == 0 and n == 0: 
            res.append(path)
        for i in xrange(index, len(nums)):
            self.dfs(nums, k-1, n-nums[i], i+1, path+[nums[i]], res)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.