8000 refactor 40 · payal-kothari/Leetcode@8279086 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8279086

Browse files
refactor 40
1 parent 217b605 commit 8279086

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ All numbers (including target) will be positive integers.
2424
public class _40 {
2525

2626
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-
}
27+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
28+
List<List<Integer>> result = new ArrayList();
29+
Arrays.sort(candidates);
30+
backtracking(candidates, 0, result, target, new ArrayList());
31+
return result;
32+
}
3333

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);
43-
}
44-
} else if (target == 0) {
45-
result.add(new ArrayList(curr));
34+
void backtracking(int[] candidates, int start, List<List<Integer>> result, int target,
35+
List<Integer> curr) {
36+
if (target > 0) {
37+
for (int i = start; i < candidates.length; i++) {
38+
if (candidates[i] > target || (i > start && candidates[i - 1] == candidates[i])) {
39+
continue;
4640
}
41+
curr.add(candidates[i]);
42+
backtracking(candidates, i + 1, result, target - candidates[i], curr);
43+
curr.remove(curr.size() - 1);
44+
}
45+
} else if (target == 0) {
46+
result.add(new ArrayList(curr));
4747
}
48+
}
4849
}
49-
50-
}
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._40;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _40Test {
13+
private static _40.Solution1 solution1;
14+
private static int[] candidates;
15+
private static List<List<Integer>> expected;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _40.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
candidates = new int[] {10, 1, 2, 7, 6, 1, 5};
25+
expected = new ArrayList<>();
26+
expected.add(Arrays.asList(1, 1, 6));
27+
expected.add(Arrays.asList(1, 2, 5));
28+
expected.add(Arrays.asList(1, 7));
29+
expected.add(Arrays.asList(2, 6));
30+
assertEquals(expected, solution1.combinationSum2(candidates, 8));
31+
}
32+
}

0 commit comments

Comments
 (0)
0