The same repeated number may be chosen from C unlimited number of times.
Example
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
Note
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
classSolution:# @param candidates, a list of integers# @param target, integer# @return a list of lists of integersdefcomb_helper(self,candidates,target,result,entry,sum):ifsum>target:returnifsum==target:tmp=[]forjinentry:tmp.append(j)result.append(tmp)returnforiincandidates:entry.append(i)self.comb_helper(candidates,target,result,entry,sum+i)entry.pop()returndefcombinationSum(self,candidates,target):# write your code hereresult=[]entry=[]ifcandidates==None:returnresultself.comb_helper(candidates,target,result,entry,0)# remove the duplicated answerret=[]foriinresult:i=sorted(i)ifinotinret:ret.append(i)returnret