Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

40. Combination Sum II

Posted on 2020-06-28 | Edited on 2021-01-26

LeetCode

Backtracking

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
class Solution {
private void util(int[] arr, int target, List<List<Integer>> res, List<Integer> ls, int start) {
if (target < 0) {
return;
} else if (target == 0) {
res.add(new ArrayList(ls));
return;
} else {
for (int i = start; i < arr.length; i++) {
if (i > start && arr[i] == arr[i - 1]){
continue;
}
ls.add(arr[i]);
util(arr, target - arr[i], res, ls, i + 1);
ls.remove(ls.size() - 1);
}
}
}

public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<>();
util(candidates, target, res, new ArrayList<>(), 0);
return res;
}
}

<1…128129130…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%