Simple and Easy Python Code, Backtracing


  • 0
    M

    class Solution(object):

    def combinationSum3(self, k, n):
    
        results=[]
        
        self.helper(0,[1,2,3,4,5,6,7,8,9],n,k,[],results)
        
        return results
        
    def helper(self,start,candidates,target,level,comb,results):
        
        if target==0 and level==0:
            results.append(comb[:])
            return
        
        for i in range(start,len(candidates)):
            
            if level>0 and target>=candidates[i]:
                comb.append(candidates[i])
                self.helper(i+1,candidates,target-candidates[i],level-1,comb,results)
                comb.pop()
                
        return

Log in to reply
 

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