Python clear backtracking solution, easy understand.


  • 0
    K
    class Solution(object):
        def combinationSum2(self, candidates, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            candidates.sort()
            res = []
            self.helper(candidates,target,[],res)
            return res
        
        def helper(self,candidates,target,path,res):
            if target == 0 and path and path not in res:
                res += [path]
            for i in range(len(candidates)):
                if candidates[i] > target: break
                self.helper(candidates[i + 1:],target - candidates[i], path + [candidates[i]],res)
            return res

Log in to reply
 

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