Skip to content

Commit 67091d2

Browse files
[N-0] refactor 40
1 parent 147c4a5 commit 67091d2

File tree

1 file changed

+19
-28
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+19
-28
lines changed
Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
import java.util.ArrayList;
64
import java.util.Arrays;
75
import java.util.List;
@@ -25,35 +23,28 @@ All numbers (including target) will be positive integers.
2523
*/
2624
public class _40 {
2725

28-
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
29-
List<List<Integer>> result = new ArrayList();
30-
Arrays.sort(candidates);
31-
backtracking(candidates, target, 0, new ArrayList(), result);
32-
return result;
33-
}
26+
public static class Solution1 {
27+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
28+
List<List<Integer>> result = new ArrayList();
29+
Arrays.sort(candidates);
30+
backtracking(candidates, target, 0, new ArrayList(), result);
31+
return result;
32+
}
3433

35-
void backtracking(int[] candidates, int target, int start, List<Integer> curr, List<List<Integer>> result) {
36-
if (target > 0) {
37-
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
38-
if (i > start && candidates[i] == candidates[i - 1]) {
39-
continue;//skip duplicates, this is one difference from Combination Sum I
34+
void backtracking(int[] candidates, int target, int start, List<Integer> curr, List<List<Integer>> result) {
35+
if (target > 0) {
36+
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
37+
if (i > start && candidates[i] == candidates[i - 1]) {
38+
continue;//skip duplicates, this is one difference from Combination Sum I
39+
}
40+
curr.add(candidates[i]);
41+
backtracking(candidates, target - candidates[i], i + 1, curr, result);//i+1 is the other difference from Combination Sum I
42+
curr.remove(curr.size() - 1);
4043
}
41-
curr.add(candidates[i]);
42-
backtracking(candidates, target - candidates[i], i + 1, curr, result);//i+1 is the other difference from Combination Sum I
43-
curr.remove(curr.size() - 1);
44+
} else if (target == 0) {
45+
result.add(new ArrayList(curr));
4446
}
45-
} else if (target == 0) {
46-
List<Integer> temp = new ArrayList(curr);
47-
result.add(temp);
4847
}
4948
}
5049

51-
public static void main(String... args) {
52-
_40 test = new _40();
53-
int[] candidates = new int[]{10, 1, 2, 7, 6, 1, 5};
54-
int target = 8;
55-
List<List<Integer>> result = test.combinationSum2(candidates, target);
56-
CommonUtils.printListList(result);
57-
}
58-
59-
}
50+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy