39 - Combination Sum
Written on October 21, 2015
Tweet
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times.
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ret = []
self.helper(candidates, ret, [], target)
return ret
def helper(self, candidates, ret, curr, target):
if target == 0:
ret.append(curr[:])
return
for i, num in enumerate(candidates):
if num <= target:
self.helper(candidates[i:], ret, curr + [num], target - num)