-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
98 lines (79 loc) · 2.85 KB
/
Copy pathSolution.java
File metadata and controls
98 lines (79 loc) · 2.85 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package combinationSum;
import java.util.*;
import static java.lang.System.out;
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List resultList = new ArrayList();
if (candidates.length == 0)
return resultList;
if (target == 0)
return resultList;
Arrays.sort(candidates);
bfs(candidates, target, resultList, new ArrayList<Integer>(), 0);
return resultList;
}
public void bfs(int[] candidates, int target, List<List<Integer>> result,
List<Integer> solset, int sum) {
if (sum > target)
return;
if (sum == target) {
Collections.sort(solset);
if (!result.contains(solset)) {
result.add(new ArrayList<Integer>(solset));
return;
}
}
for (int i = 0; i < candidates.length; i++) {
solset.add(candidates[i]);
sum = sum + candidates[i];
bfs(candidates, target, result, solset, sum);
sum = sum - candidates[i];
solset.remove(candidates[i]);
}
}
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static String integerArrayListToString(List<Integer> nums, int length) {
if (length == 0) {
return "[]";
}
String result = "";
for(int index = 0; index < length; index++) {
Integer number = nums.get(index);
result += Integer.toString(number) + ", ";
}
return "[" + result.substring(0, result.length() - 2) + "]";
}
public static String integerArrayListToString(List<Integer> nums) {
return integerArrayListToString(nums, nums.size());
}
public static String int2dListToString(List<List<Integer>> nums) {
StringBuilder sb = new StringBuilder("[");
for (List<Integer> list: nums) {
sb.append(integerArrayListToString(list));
sb.append(",");
}
sb.setCharAt(sb.length() - 1, ']');
return sb.toString();
}
public static void main(String[] args) {
String line = "[2,3,6,7]";
int[] candidates = stringToIntegerArray(line);
int target = 7;
List<List<Integer>> ret = new Solution().combinationSum(candidates, target);
String out = int2dListToString(ret);
System.out.print(out);
}
}