Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution {
List<List<Integer>> res=new ArrayList();
public List<List<Integer>> subsetsWithDup(int[] nums) {
if(nums.length==0)
return res;
Arrays.sort(nums);
findSubsetsWithDup(nums,0,new ArrayList<Integer>(nums.length));
return res;
}
private void findSubsetsWithDup(int[]nums,int starts,ArrayList<Integer> c){
if(c.size()==nums.length){
res.add(new ArrayList(0));
return;
}
for(int i=starts;i<nums.length;i++){
if(i>starts&&i>0&&nums[i]==nums[i-1])
continue;
c.add(nums[i]);
res.add(new ArrayList(c));
findSubsetsWithDup(nums,i+1,c);
c.remove(c.size()-1);
}
}
}