Python Solution beating 99.9 %


  • 0
    S
     def combinationSum(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            a= sorted(candidates)
            l = len(a)
            result = []
            
            def dfs(remain, ans, index):
                if remain == 0 :
                    result.append(ans)
                    return
                for i in xrange(index,l):
                    if a[i] > remain: break
                    dfs(remain - a[i], ans+[a[i]], i)
                    
            dfs(target, [], 0)
            return result 
    

Log in to reply
 

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