Given an integer array nums that may contain duplicates, return all possible
subsets
(the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
public void subsetsDuplicate(int[] nums, List<List<Integer>> res, int index, List<Integer> currRes) {
res.add(new ArrayList<>(currRes));
for(int i=index; i<nums.length;i++){
if(i!=index && nums[i]==nums[i-1])
continue;
currRes.add(nums[i]);
subsetsDuplicate(nums, res, i+1, currRes);
currRes.remove(currRes.size()-1);
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
subsetsDuplicate(nums, res, 0, new ArrayList<>());
return res;
}
}