My Python code, any improvements?


  • 0
    H
    class Solution:
        # @param {integer} k
        # @param {integer} n
        # @return {integer[][]}
        def combinationSum3(self, k, n):
            res = []
            def dfs(k, n, start, curr):
                if start > n:
                    return
                print k, n, start, curr
                if k == 1:
                    if n not in curr and n <= 9:
                        curr.add(n)
                        res.append(list(curr))
                        curr.remove(n)
                    return
                for i in range(start, 9 + 1):
                    curr.add(i)
                    dfs(k - 1, n - i, i + 1, curr)
                    curr.remove(i)
            dfs(k, n, 1, set())
            return res

  • 0
    C

    At least you can remove the line "print k, n, start, curr".


Log in to reply
 

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