forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombinationSum.java
63 lines (56 loc) · 1.8 KB
/
CombinationSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package backtracking;
import java.util.ArrayList;
import java.util.List;
/**
* Created by pradhang on 3/14/2017.
Given a set of candidate numbers (C) (without duplicates) 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.
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]
]
*/
public class CombinationSum
{
/**
* Main method
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
int[] candidates = {2,3,6,7};
List<List<Integer>> result = new CombinationSum().combinationSum(candidates, 7);
}
public List<List<Integer>> combinationSum(int[] candidates, int target)
{
List<List<Integer>> result = new ArrayList<>();
List<Integer> subList = new ArrayList<>();
doNext(0, result, 0, candidates, target, subList);
return result;
}
private void doNext(int i, List<List<Integer>> result, int count, int[] candidates, int target, List<Integer> subArr)
{
if(target == 0)
{
List<Integer> subList = new ArrayList<>();
for(int k = 0; k < count; k ++)
subList.add(subArr.get(k));
result.add(subList);
}
else if(target > 0)
{
for(int j = i, l = candidates.length; j < l; j ++)
{
subArr.add(candidates[j]);
doNext(j, result, count + 1, candidates, target - candidates[j], subArr);
subArr.remove(subArr.size() - 1);
}
}
}
}