Skip to content

Commit 7af17b5

Browse files
committed
refine
1 parent 52b74e9 commit 7af17b5

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

CombinationSum.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
import java.util.ArrayList;
43
import java.util.Arrays;
54

@@ -11,14 +10,13 @@
1110
*
1211
* Note:
1312
*
14-
* All numbers (including target) will be positive integers.
13+
* All numbers (including target) will be positive integers.
1514
*
16-
* Elements in a combination (a1, a2, ... , ak) must be in non-descending order. (ie, a1 <= a2 <= ... <= ak).
15+
* Elements in a combination (a1, a2, ... , ak) must be in non-descending order.
16+
* (ie, a1 <= a2 <= ... <= ak).
1717
*
18-
* The solution set must not contain duplicate combinations.
19-
* For example, given candidate set 2,3,6,7 and target 7, A solution set is:
20-
* [7]
21-
* [2, 2, 3]
18+
* The solution set must not contain duplicate combinations. For example, given
19+
* candidate set 2,3,6,7 and target 7, A solution set is: [7] [2, 2, 3]
2220
*/
2321

2422
public class CombinationSum {
@@ -38,20 +36,12 @@ private void combinationSum(int[] candidates, int start, int sum,
3836
ret.add(new ArrayList<Integer>(solution));
3937
return;
4038
}
41-
if (start > candidates.length - 1)
39+
if (sum > target)
4240
return;
43-
int times = 0;
44-
while (true) {
45-
if (sum > target) {
46-
for (int h = 0; h < times; h++) {
47-
solution.remove(solution.size() - 1);
48-
}
49-
break;
50-
}
51-
combinationSum(candidates, start + 1, sum, target, ret, solution);
52-
sum += candidates[start];
53-
solution.add(candidates[start]);
54-
times++;
41+
for (int i = start; i < candidates.length; i++) {
42+
solution.add(candidates[i]);
43+
combinationSum(candidates, start, sum + candidates[i], target, ret, solution);
44+
solution.remove(solution.size() - 1);
5545
}
5646
}
5747
}

CombinationSumII.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
3+
*
4+
* Each number in C may only be used once in the combination.
5+
*
6+
* Note:
7+
*
8+
* All numbers (including target) will be positive integers.
9+
*
10+
* Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
11+
* The solution set must not contain duplicate combinations.
12+
* For example, given candidate set 10,1,2,7,6,1,5 and target 8, A solution set is:
13+
* [1, 7]
14+
* [1, 2, 5]
15+
* [2, 6]
16+
* [1, 1, 6]
17+
*/
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
22+
public class CombinationSumII {
23+
public ArrayList<ArrayList<Integer>> combinationSum2(int[] candidates,
24+
int target) {
25+
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
26+
ArrayList<Integer> list = new ArrayList<Integer>();
27+
Arrays.sort(candidates);
28+
dfs(res, 0, candidates, list, target);
29+
return res;
30+
}
31+
32+
private void dfs(ArrayList<ArrayList<Integer>> res, int start,
33+
int[] candidates, ArrayList<Integer> list, int target) {
34+
if (target == 0) {
35+
res.add(new ArrayList<Integer>(list));
36+
return;
37+
}
38+
int pre = -1;
39+
for (int i = start; i < candidates.length; i++) {
40+
if (pre == candidates[i])
41+
continue;
42+
if (candidates[i] > target)
43+
return;
44+
pre = candidates[i];
45+
list.add(candidates[i]);
46+
dfs(res, i + 1, candidates, list, target - candidates[i]);
47+
list.remove(list.size() - 1);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)