[LeetCode] Subsets II

90. Subsets II

Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> subsets{{}};
for(auto& n : nums) {
int sz = subsets.size();
for(int i = 0; i < sz; i++) {
vector<int> s = subsets[i];
s.push_back(n);
subsets.push_back(s);
}
}
set<vector<int>> s(subsets.begin(), subsets.end());
return vector<vector<int>>(s.begin(), s.end());
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/30/PS/LeetCode/subsets-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.