Simple python solution


  • 1
    Y
    class Solution(object):
    def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        return list(self.solve(sorted(candidates), target))
    
    def solve(self,ls,target):
        for i,item in enumerate(ls):
            if target > item:
                for it in self.solve(ls[i:],target-item):
                    yield [item]+it
            elif target == item:
                yield [item]

Log in to reply
 

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