-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (36 loc) · 1.23 KB
/
Solution.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
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<String> temp = new ArrayList<String>();
List<List<Integer>> res = new ArrayList<List<Integer>>();
backTrack(candidates, new ArrayList<Integer>(), target, temp);
for (String s : temp) {
List<Integer> t = new ArrayList<Integer>();
for (String c : s.split(",")) {
t.add(Integer.valueOf(c));
}
res.add(t);
}
return res;
}
public void backTrack(int[] candidates, List<Integer> a, int target, List<String> t) {
if (target == 0) {
Collections.sort(a);
List<String> l = new ArrayList<String>();
for (Integer i : a) {
l.add(String.valueOf(i));
}
String s = String.join(",", l);
if (!t.contains(s)) {
t.add(s);
}
} else {
for(int c : candidates) {
if (c <= target) {
List<Integer> temp = new ArrayList<Integer>(a);
temp.add(c);
backTrack(candidates, temp, target - c, t);
}
}
}
}
}