Simple Python Solution

  • 0
    class Solution(object):
        def combinationSum3(self, k, n):
            if k<1 or k> 9 or n<1 or n>45:
                return []
            path, res, index = [], [], 1
            self.dfs(k, n, index, path, res)
            return res
        def dfs(self, k, n, index, path, res):
            if n < 0 or len(path)>k:
            if n == 0 and len(path)==k:
            for i in range(index, 10):
                self.dfs(k, n-i, i+1, path+[i], res)

Log in to reply

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