-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubSetsll.java
More file actions
28 lines (22 loc) · 783 Bytes
/
Copy pathSubSetsll.java
File metadata and controls
28 lines (22 loc) · 783 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> answer = new ArrayList<>();
Arrays.sort(nums);
backtrack(answer, new ArrayList<>(), nums, 0);
return answer;
}
private void backtrack(List<List<Integer>> answer, List<Integer> currSet, int[] nums, int index) {
answer.add(new ArrayList<>(currSet));
for (int i = 0; i < nums.length; i++) {
if (i != index && nums[i] == nums[i - 1]) {
continue;
}
currSet.add(nums[i]);
backtrack(answer, currSet, nums, index+1);
currSet.remove(currSet.size()-1);
}
}
}