Python neat solution. Not fast enough though (55%)


  • 0
    M

    Compared to the combination I, changed a little:

    class Solution(object):
        def combinationSum2(self, candidates, target):
            nums = sorted(candidates)
            return self.comb(nums, target)
            
        def comb(self, nums, target):
            if not nums or nums[0]>target: return []
            elif nums[0] == target: return [[target]]
            return [[nums[0]]+elem for elem in self.comb(nums[1:], target-nums[0])] + self.comb([x for x in nums[1:] if x>nums[0]], target) 
    

Log in to reply
 

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