# LeetCode – Combination Sum (Java)

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.

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. For example, given candidate set 2,3,6,7 and target 7, A solution set is:

```[7]
[2, 2, 3]
```

Thoughts

The first impression of this problem should be depth-first search(DFS). To solve DFS problem, recursion is a normal implementation.

Note that the candidates array is not sorted, we need to sort it first.

Java Solution

```public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();   if(candidates == null || candidates.length == 0) return result;   ArrayList<Integer> current = new ArrayList<Integer>(); Arrays.sort(candidates);   combinationSum(candidates, target, 0, current, result);   return result; }   public void combinationSum(int[] candidates, int target, int j, ArrayList<Integer> curr, ArrayList<ArrayList<Integer>> result){ if(target == 0){ ArrayList<Integer> temp = new ArrayList<Integer>(curr); result.add(temp); return; }   for(int i=j; i<candidates.length; i++){ if(target < candidates[i]) return;   curr.add(candidates[i]); combinationSum(candidates, target - candidates[i], i, curr, result); curr.remove(curr.size()-1); } }```
Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
1. Lean Lee on 2015-7-23

I wonder why we need make a temp ArrayList and then copy the curr Arraylist and then add temp into result, why just add curr into the result directly?

2. Jeffrey Ni on 2015-10-21

Java always pass parameters by value. Here curr is a refferrece to the array object, if you don’t copy it but add it to the result directly, later when you modify curr, it will change the list inside result. You may end up with all same lists in result. You can self-test it.

3. Dexter on 2015-10-23

public static void sum(int[] set,int s){

int [][]dp = new int[s+1][set.length];

for(int i=0;i < set.length; i++){

for(int j = 0; j j){

dp[j][i] = 0;

continue;

}

if(set[i] == j){

dp[j][i] = i+1;

continue;

}

int remaining = j-set[i];

int k = i;

while(k >= 0){

if(dp[remaining][k] > 0){

dp[j][i] = k+1;

break;

}

k–;

}

}

}

int l=0;

while(l 0){

int sum = s;

int col = l;

System.out.print(“[“);

while(sum > 0){

System.out.print(set[col]+”,”);

int temp = col;

col = dp[sum][col]-1;

sum -= set[temp];

}

System.out.print(“]”);

}

l++;

System.out.println(“”);

}

}

4. Surbhi Motghare on 2016-2-14

Great solution. why are we removing the last element from curr. (last line where curr.remove(curr.size()-1) ? )

5. divya on 2016-3-11

Shouldn’t it be i+1, 3rd parameter here->combinationSum(candidates, target – candidates[i], i, curr, result);

6. ryanlr on 2016-4-25

I don’t think so, because duplicates are allowed in a combination set, i. e., the same number csn be used twice.

7. gowtham Bk on 2016-7-12

Why can’t we just add curr to result ? Why do we use temp ?

8. Didier on 2016-8-3

This solution is incorrect. If candidates are [2, 3, 3, 6, 7]. The returned lists would be:
[2, 2, 3]
[2, 2, 3]
[7]

May need to add some condition to exclude the repeated entry in candidates.

9. Александр Ефремов on 2016-10-1

Because this is the main idea of backtracking – try option (curr.add(candidate)), then backtrack – curr.remove(cur.size()-1)

10. pjpal on 2016-12-15

What would be the running time of this algorithm?

11. Derek on 2017-8-14

This solution doesn’t handle duplicate numbers in the list. Duplicates should be stripped out or logic needs to account for it.