class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> res;
vector<int> record;
vector<bool> used(nums.size(), false);
sort(nums.begin(), nums.end());
for (int i = 1; i <= nums.size(); i++) {
getSubsets(nums, res, i, 0, record, used);
}
res.push_back(vector<int>());
return res;
}
private:
void getSubsets(vector<int>& nums, vector<vector<int>>& res, int n, int start, vector<int>& record, vector<bool>& used) {
if (record.size() == n) {
res.push_back(record);
return;
}
for (int i = start; i < nums.size(); i++) {
if (!used[i]) {
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
continue;
}
record.push_back(nums[i]);
used[i] = true;
getSubsets(nums, res, n, i + 1, record, used);
record.pop_back();
used[i] = false;
}
}
}
};