-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLeetCode90.java
More file actions
34 lines (31 loc) · 1014 Bytes
/
LeetCode90.java
File metadata and controls
34 lines (31 loc) · 1014 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
30
31
32
33
34
package problems;
import java.util.*;
public class LeetCode90 {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> ret = new ArrayList<>();
Deque<Integer> ans = new ArrayDeque<>();
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
dfs(nums, used, 0, nums.length, ret, ans);
return ret;
}
private void dfs(int[] nums, boolean[] used, int begin, int length, List<List<Integer>> ret, Deque<Integer> ans) {
ret.add(new ArrayList<>(ans));
if(ans.size() == length){
return;
}
for (int i = begin; i < length; i++) {
if(used[i]){
continue;
}
if(i > begin && nums[i] == nums[i-1] && !used[i]){
continue;
}
used[i] = true;
ans.add(nums[i]);
dfs(nums, used, begin + 1, length, ret, ans);
ans.removeLast();
used[i] = false;
}
}
}