-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSumII.java
More file actions
46 lines (35 loc) · 1.46 KB
/
CombinationSumII.java
File metadata and controls
46 lines (35 loc) · 1.46 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
/*
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[ [1,1,6], [1,2,5], [1,7], [2,6] ]
*/
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> cnd = new ArrayList<>();
Arrays.sort(candidates);
helper(candidates, 0, cnd, res, target);
return res;
}
private void helper(int[] nums, int i, List<Integer> sub, List<List<Integer>> res, int target){
if(target == 0){
res.add(new ArrayList<>(sub));
return;
}
else if(i < nums.length && target > 0){
sub.add(nums[i]);
helper(nums, i + 1, sub, res, target - nums[i]);
sub.remove(sub.size() - 1);
//moving index to the element which is not in sub list
//it removes problem of duplicating sets in res list
// but 1st sort the candidates in order to do so
int temp = nums[i];
while(i < nums.length && nums[i] == temp) i++;
helper(nums, i, sub, res, target);
}
}
}