Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
和Subset解法一样,用个set去重,放进去之前先排序,不然不同序的vector是代表不同的元素
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector< vector< int> > res;
set< vector< int> > s;
size_t upper_bounds = 1 << S.size();
for( size_t i = 0; i < upper_bounds; ++i){
vector< int> tmp;
size_t index = 0;
size_t j = i;
while(j){
if( j & 1)
tmp.push_back(S[index]);
j >>= 1;
++index;
}
sort( tmp.begin(), tmp.end());
s.insert(tmp);
}
for( set< vector< int> >::iterator iter = s.begin(); iter != s.end(); ++iter){
res.push_back(*iter);
}
sort( res.begin(), res.end());
return res;
}
};