深度优先遍历,通过set进行去重:
// // Created by jt on 2020/8/25. // class Solution { public: vector<vector<int> > subsetsWithDup(vector<int> &S) { sort(S.begin(), S.end()); vector<int> vec; set<vector<int> > sv; dfs(S, vec, sv, 0); return vector<vector<int> >(sv.begin(), sv.end()); } void dfs(vector<int> &S, vector<int> vec, set<vector<int> > &sv, int begin) { if (begin >= S.size()) { sv.insert(vec); return; } dfs(S, vec, sv, begin + 1); vec.push_back(S[begin]); dfs(S, vec, sv, begin + 1); } };