Python recursive solution


  • 0
    D
    class Solution(object):
        def combinationSum2(self, candidates, target):
            results = set()
            self._combinationSum2(sorted(candidates, reverse=True), target, results, (), 0)
            results = [list(result) for result in results]
            return results
            
        def _combinationSum2(self, candidates, target, results, progress, sum):
            if sum == target:
                results.add(progress)
                return
            if sum > target or not candidates:
                return
            for i, c in enumerate(candidates):
                new_progress = progress + (c,)
                new_sum = sum + c
                self._combinationSum2(candidates[i+1:], target, results, new_progress, new_sum)
    

Log in to reply
 

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