Python with backtracking


  • 0
    F
        def combinationSum3(self, k, n):
            """
            :type k: int
            :type n: int
            :rtype: List[List[int]]
            """
            self.ans = []
            self.dfs(1, n, [], k)
            return self.ans
        def dfs(self, start, target, valuelist, k):
            if target == 0 and len(valuelist) == k:
                self.ans.append(valuelist)
                return
            if target <= 0:
                return
            for i in range(start, 10):
                self.dfs(i + 1, target - i, valuelist + [i], k)

Log in to reply
 

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