Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: 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],
[]
]
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums)
{
vector<vector<int> >ret;
if(nums.empty())
return ret;
sort(nums.begin(),nums.end());
vector<int>temp;
ret.push_back(temp);
dfs(nums,0,temp,ret);
return ret;
}
void dfs(vector<int>& nums,int pos,vector<int>&temp,vector<vector<int> >&ret)
{
if(pos==nums.size())
return;
for(int i=pos;i<nums.size();i++)
{
if(i!=pos&&nums[i]==nums[i-1])
continue;
temp.push_back(nums[i]);
ret.push_back(temp);
dfs(nums,i+1,temp,ret);
temp.pop_back();
}
}
};