Simple python


  • 0
    W

    Use "choice = range( 1,10) if len(used) == 0 else range( max(used)+1,10)" to avoid duplicate combinations

    class Solution(object):
        def combinationSum3(self, k, n):
            """
            :type k: int
            :type n: int
            :rtype: List[List[int]]
            """
            self.reslist = []
            self.helper([],k,n)
            return self.reslist
        
        def helper(self, used, k, n):
            if k == 0 and n == 0:
                self.reslist.append(used)
                return
            choice = range( 1,10) if len(used) == 0 else range( max(used)+1,10)
            for i in choice:
                if k > 0 and n > 0:
                    self.helper(used + [i], k-1, n - i)
                else:
                    return
    

Log in to reply
 

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