-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0040-combination-sum-ii.java
More file actions
29 lines (27 loc) · 924 Bytes
/
0040-combination-sum-ii.java
File metadata and controls
29 lines (27 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
List<Integer> ls = new ArrayList<Integer>();
comb(candidates, target, ans, ls, 0);
return ans;
}
public void comb(
int[] candidates,
int target,
List<List<Integer>> ans,
List<Integer> ls,
int index
) {
if (target == 0) {
ans.add(new ArrayList(ls));
} else if (target < 0) return; else {
for (int i = index; i < candidates.length; i++) {
if (i > index && candidates[i] == candidates[i - 1]) continue;
ls.add(candidates[i]);
comb(candidates, target - candidates[i], ans, ls, i + 1);
ls.remove(ls.get(ls.size() - 1));
}
}
}
}