Skip to content

Commit d1c7683

Browse files
authored
Update Combination Sum II.java
1 parent c6f4a2a commit d1c7683

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

Medium/Combination Sum II.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
class Solution {
22
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
3-
List<List<Integer>> list = new ArrayList<>();
3+
List<List<Integer>> result = new ArrayList<>();
44
Arrays.sort(candidates);
5-
helper(candidates, 0, target, new ArrayList<>(), list);
6-
return list;
5+
helper(candidates, target, result, new ArrayList<>(), 0);
6+
return result;
77
}
88

9-
private void helper(int[] candidates, int idx, int target, List<Integer> temp, List<List<Integer>> list) {
10-
if (target == 0) {
11-
list.add(new ArrayList<>(temp));
12-
return;
13-
}
14-
if (target < 0) {
9+
private void helper(int[] candidates, int target, List<List<Integer>> result, List<Integer> currCombination, int idx) {
10+
if (target <= 0) {
11+
if (target == 0) {
12+
result.add(new ArrayList<>(currCombination));
13+
}
1514
return;
1615
}
1716
for (int i = idx; i < candidates.length; i++) {
18-
if (i > idx && candidates[i] == candidates[i - 1]) {
17+
if (i > idx && candidates[i] == candidates[i - 1]) {
1918
continue;
2019
}
21-
temp.add(candidates[i]);
22-
helper(candidates, i + 1, target - candidates[i], temp, list);
23-
temp.remove(temp.size() - 1);
20+
currCombination.add(candidates[i]);
21+
helper(candidates, target - candidates[i], result, currCombination, i + 1);
22+
currCombination.remove(currCombination.size() - 1);
2423
}
2524
}
2625
}

0 commit comments

Comments
 (0)