# 90. Subsets II
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],
[]
]
# Solution
Approach 1: Based on the size of each subset.
# Code (Python)
Approach 1:
# Code (C++)
Approach 1:
class Solution {
private:
vector<vector<int>> subsets;
void doSubsetsWithDup(vector<int>& nums, int head, int level, vector<int>& buf) {
if (level == 0)
{
subsets.push_back(buf);
return;
}
for (int i = head; i <= nums.size() - level; ++i)
{
if (i > head && nums[i] == nums[i-1])
continue;
buf.push_back(nums[i]);
doSubsetsWithDup(nums, i + 1, level - 1, buf);
buf.pop_back();
}
}
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
std::sort(nums.begin(), nums.end());
vector<int> buf;
for (int i = 0; i <= nums.size(); ++i)
{
doSubsetsWithDup(nums, 0, i, buf);
}
return subsets;
}
};