Question
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Code
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
Set<List<Integer>> sresults = new HashSet<>();
List<List<Integer>> results = new ArrayList<>();
int len = (int) Math.pow(2, nums.length);
boolean[] flags = new boolean[nums.length];
for (int i = 0; i < len; i++) {
for (int j = 0; j < flags.length; j++) {
if (!flags[j]) {
flags[j] = true;
break;
}
flags[j] = false;
}
List<Integer> sub = new ArrayList<>();
for (int j = 0; j < flags.length; j++) {
if (flags[j]) {
sub.add(nums[j]);
}
}
sresults.add(sub);
}
for (List<Integer> sub : sresults) {
results.add(sub);
}
return results;
}