Skip to content

Commit ba06e19

Browse files
[N-0] refactor 40
1 parent 1a2d952 commit ba06e19

File tree

1 file changed

+10
-8
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+10
-8
lines changed

src/main/java/com/fishercoder/solutions/_40.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import java.util.ArrayList;
66
import java.util.Arrays;
77
import java.util.List;
8-
/***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.
9-
10-
Each number in C may only be used once in the combination.
8+
/**
9+
* 40. Combination Sum II
10+
*
11+
* 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.
12+
* Each number in C may only be used once in the combination.
1113
1214
Note:
1315
All numbers (including target) will be positive integers.
@@ -19,24 +21,25 @@ All numbers (including target) will be positive integers.
1921
[1, 2, 5],
2022
[2, 6],
2123
[1, 1, 6]
22-
]*/
24+
]
25+
*/
2326
public class _40 {
2427

2528
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
2629
List<List<Integer>> result = new ArrayList();
2730
Arrays.sort(candidates);
28-
helper(candidates, target, 0, new ArrayList(), result);
31+
backtracking(candidates, target, 0, new ArrayList(), result);
2932
return result;
3033
}
3134

32-
void helper(int[] candidates, int target, int start, List<Integer> curr, List<List<Integer>> result) {
35+
void backtracking(int[] candidates, int target, int start, List<Integer> curr, List<List<Integer>> result) {
3336
if (target > 0) {
3437
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
3538
if (i > start && candidates[i] == candidates[i - 1]) {
3639
continue;//skip duplicates, this is one difference from Combination Sum I
3740
}
3841
curr.add(candidates[i]);
39-
helper(candidates, target - candidates[i], i + 1, curr, result);//i+1 is the other difference from Combination Sum I
42+
backtracking(candidates, target - candidates[i], i + 1, curr, result);//i+1 is the other difference from Combination Sum I
4043
curr.remove(curr.size() - 1);
4144
}
4245
} else if (target == 0) {
@@ -53,5 +56,4 @@ public static void main(String... args) {
5356
CommonUtils.printListList(result);
5457
}
5558

56-
5759
}

0 commit comments

Comments
 (0)