# 【LeetCode】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],
[]
]

class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
set<vector<int>> result_set;

for (int i = 0; i <= nums.size(); ++ i) {
vector<int> current;
dfs(nums, 0, i, current, result_set);
}
vector<vector<int>> result;
result.assign(result_set.begin(), result_set.end());
return result;
}
void dfs(const vector<int>& nums, int current_index, int count, vector<int> &current, set<vector<int>>& result) {
if (!count) {
result.insert(current);
return;
}
if (current_index >= nums.size()) {
return;
}

dfs(nums, current_index + 1, count, current, result);

current.push_back(nums[current_index]);
dfs(nums, current_index + 1, count - 1, current, result);
current.pop_back();
}
};

07-05 1283
01-15 1920

05-23 383
08-04 155
04-08 1118
06-21 1419
04-12 1150
05-27 962
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客