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 is chosen from C unlimited number of times.
Note:
- All numbers (including target) would be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set and [2, 3, 6, 7]
target 7
,
A Solution set is:
[ 7], [2, 2, 3]]
1 Public classSolution {2 Publiclist<list<integer>> ans =NewArraylist<list<integer>>();3 PublicList<list<integer>> Combinationsum (int[] candidates,inttarget) {4list<integer> list =NewArraylist<integer>();5Combination (list,candidates,target,0,0);6 returnans;7 }8 9 Public voidCombination (list<integer> List,int[] Can,intTargetintSumints) {Ten if(Sum = =target) { Onelist<integer> tmp =NewArraylist<integer>(list); A Ans.add (TMP); - return; - } the if(Sum > Target)return; - for(inti = s; i < can.length; i++){ - List.add (Can[i]); -Combination (list,can,target,sum+can[i],i); +List.remove (List.size ()-1); - } + } A}
It's all a routine. N Queen problem mastered just OK
Combination Sum Java Solutions