递归暴力枚举就好了- -
vector<pair<int, int> > data;
class Solution {
void dfs(int cur, vector<int> & stk,
vector<vector<int> > &ret){
if(cur >= data.size()){
ret.push_back(stk);
return ;
}
dfs(cur + 1, stk, ret);
for(int i = 0; i < data[cur].second; ++ i){
stk.push_back(data[cur].first);
dfs(cur + 1, stk, ret);
}
for(int i = 0; i < data[cur].second; ++ i)
stk.pop_back();
}
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<int, int> hash;
for(int i = 0; i < S.size(); ++ i)
hash[S[i]] ++ ;
data.clear();
for(map<int, int> :: iterator it = hash.begin();
it != hash.end(); ++ it)
data.push_back(make_pair(it->first, it->second));
vector<vector<int> > ret;
vector<int> stk;
dfs(0, stk, ret);
return ret;
}
};