Concise and Easy Understanding Python Solution


  • 1
    I
    def combinationSum3(self, k, n):
        self.solution = []
        self.helper(1, k, n, [])
        return self.solution
        
    def helper(self,start, k, n, result):
        if k == 0 and n == 0:
            self.solution.append(result[:])
        for num in range(start, 10):
            result.append(num)
            if n-num >= 0:
                self.helper(num+1, k - 1, n-num, result)    
            result.pop()

  • 0
    S

    what is the purpose to add [:] after result ?


  • 0
    I

    result[:] is like new result(). If we don't do this, result would be same over all the program.


Log in to reply
 

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