Simple and Clear Python 3 Solution

  • 0
    class Solution(object):
        def combinationSum3(self, k, n):
            :type k: int
            :type n: int
            :rtype: List[List[int]]
            if not k > 0:
                return []
            ans = []
            self.solver(k, n, 1, 0, [], ans)
            return ans
        def solver(self, k, n, idx, max_used, part_ans, ans):
            if idx > k:
                if sum(part_ans) == n:
            for i in range(max_used + 1, 9 + 1):
                tmp = part_ans.copy()   # Cannot be used in Python 2; try part_ans[:]
                if sum(tmp) <= n:
                    self.solver(k, n, idx + 1, i, tmp, ans)

    I don't think I need to explain anything. But please not that you can't use copy() on list data structure in Python 2.

Log in to reply

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