# [Lintcode] Combination Sum

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.

For example, given candidate set 2,3,6,7 and target 7
A solution set is:
[7]
[2, 2, 3]

##### Notice
• All numbers (including target) will be positive integers.
• Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
• The solution set must not contain duplicate combinations.
Example

given candidate set 2,3,6,7 and target 7
A solution set is:
[7]
[2, 2, 3]

public class Solution {
/**
* @param candidates: A list of integers
* @param target:An integer
* @return: A list of lists of integers
*/
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<List<Integer>>();
helper(res, new ArrayList<Integer>(), candidates, target, 0);
return res;
}
void helper(List<List<Integer>> res, List<Integer> list, int[] candidates, int target, int index) {
if(target == 0 && !res.contains(list)) {
return;
}
if(index >= candidates.length || target < 0)
return;

for(int i = index; i < candidates.length; i++) {
helper(res, list, candidates, target - candidates[i], i);
list.remove(list.size() - 1);
}
}
}

07-29 4534

09-28 326

07-25 1277

07-06 3906

01-31 795

11-25 1562

03-15 1326

09-22 861

06-16 942

05-31 3614