本文主要是介绍[leetcode刷题系列]Subsets II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
递归暴力枚举就好了- -
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() functionmap<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;}
};
这篇关于[leetcode刷题系列]Subsets II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!