这个题真的是这么做的吗!!
——把Subset里的结果中所有重复的删掉就OK了。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int>> res;
vector<int> path;
for (int i = 0; i <= S.size(); ++i)
helper(S, res, i, 0, path);
set<vector<int>> tmp;
for (int i = 0; i < res.size(); ++i)
tmp.insert(res[i]);
res.clear();
set<vector<int>>::iterator it;
for (it = tmp.begin(); it != tmp.end(); it++)
res.push_back(*it);
return res;
}
void helper(vector<int> &S, vector<vector<int>> &res, int count, int start, vector<int> &path) {
if(count == 0) {
res.push_back(path);
return;
}
for (int i = start; i < S.size(); ++i) {
path.push_back(S[i]);
helper(S, res, count - 1, i + 1, path);
path.pop_back();
}
}
};
http://oj.leetcode.com/problems/subsets-ii/