- Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathCombinationSumII.java
29 lines (23 loc) · 901 Bytes
/
CombinationSumII.java
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
//LeetCode 40. Combination Sum II
//Question - https://leetcode.com/problems/combination-sum-ii/
classSolution {
publicList<List<Integer>> combinationSum2(int[] nums, inttarget) {
List<List<Integer>> res = newArrayList<>();
Arrays.sort(nums);
helper(nums, 0, 0, target, newArrayList<>(), res);
returnres;
}
publicvoidhelper(intnums[], intindex, intsum, inttarget, List<Integer> l, List<List<Integer>> res){
if(sum > target) return;
elseif(sum == target){
res.add(newArrayList<>(l));
return;
}
for(inti = index ; i < nums.length ; i++){
if(i > index && nums[i] == nums[i-1]) continue;
l.add(nums[i]);
helper(nums, i + 1, sum + nums[i], target, l, res);
l.remove(l.size()-1);
}
}
}