Skip to content

Commit c5d910d

Browse files
author
ChienkuChen
committed
Redo 39
1 parent 8682c4c commit c5d910d

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

src/_39/39.combination-sum.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
package _39;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
54
import java.util.List;
65

76
class Solution {
87
public List<List<Integer>> combinationSum(int[] candidates, int target) {
9-
Arrays.sort(candidates);
10-
return find(candidates, target, 0);
8+
if (candidates == null || candidates.length == 0)
9+
return new ArrayList<>();
10+
11+
List<List<Integer>> result = new ArrayList<>();
12+
13+
helper(candidates, target, 0, new ArrayList<>(), result);
14+
15+
return result;
1116
}
1217

13-
public List<List<Integer>> find(int[] candidates, int target, int start) {
14-
if (target <= 0)
15-
return new ArrayList<>();
18+
private void helper(int[] candidates, int target, int start, List<Integer> tmp, List<List<Integer>> result) {
19+
if (target < 0)
20+
return;
1621

17-
List<List<Integer>> results = new ArrayList<>();
18-
for (int i = start; i < candidates.length; i++) {
19-
if (target - candidates[i] == 0) {
20-
List<Integer> list = new ArrayList<>();
21-
list.add(candidates[i]);
22-
results.add(list);
23-
return results;
24-
}
25-
26-
List<List<Integer>> lists = find(candidates, target - candidates[i], i);
27-
for (List<Integer> list : lists) {
28-
list.add(candidates[i]);
29-
results.add(list);
30-
}
22+
if (target == 0) {
23+
result.add(tmp);
24+
return;
3125
}
3226

33-
return results;
27+
if (start >= candidates.length)
28+
return;
29+
30+
helper(candidates, target, start + 1, tmp, result);
31+
32+
List<Integer> list = new ArrayList<>(tmp);
33+
list.add(candidates[start]);
34+
helper(candidates, target - candidates[start], start, list, result);
3435
}
3536

3637
public static void main(String[] args) {
38+
System.out.println(new Solution().combinationSum(new int[]{2, 3, 5}, 8));
3739
System.out.println(new Solution().combinationSum(new int[]{2, 3, 6, 7}, 7));
3840
}
39-
}
41+
}

src/_39/39.combination-sum.java.bak

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package _39;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
class Solution {
8+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
9+
Arrays.sort(candidates);
10+
return find(candidates, target, 0);
11+
}
12+
13+
public List<List<Integer>> find(int[] candidates, int target, int start) {
14+
if (target <= 0)
15+
return new ArrayList<>();
16+
17+
List<List<Integer>> results = new ArrayList<>();
18+
for (int i = start; i < candidates.length; i++) {
19+
if (target - candidates[i] == 0) {
20+
List<Integer> list = new ArrayList<>();
21+
list.add(candidates[i]);
22+
results.add(list);
23+
return results;
24+
}
25+
26+
List<List<Integer>> lists = find(candidates, target - candidates[i], i);
27+
for (List<Integer> list : lists) {
28+
list.add(candidates[i]);
29+
results.add(list);
30+
}
31+
}
32+
33+
return results;
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println(new Solution().combinationSum(new int[]{2, 3, 6, 7}, 7));
38+
}
39+
}

0 commit comments

Comments
 (0)