Easy Recursive Python Code


  • 0
    W
    class Solution(object):
        def combinationSum(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            if target==0:
                return [[]]
            candidates.sort(reverse=True)
            re=[]
            for i,x in enumerate(candidates):
                for j in range(1,target/x+1):
                    for c in self.combinationSum(candidates[i+1:],target-j*x):
                        re.append([x for k in range(j)]+c)
            return re

Log in to reply
 

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