6ms c++ solution in 10 lines


  • 0
    S

    Group the same number and enum the count.

        void solve(map<int,int>&m, map<int,int>::iterator ite, vector<int> curr, vector<vector<int>>& v) {
            if(ite==m.end()) {
                v.push_back(curr);
                return;
            }
            for(int i=0;i<=ite->second;++i) {
                solve(m, std::next(ite), curr, v);
                curr.push_back(ite->first);
            }
        } 
        vector<vector<int>> subsetsWithDup(vector<int>& nums) {
            map<int,int> m;
            for(int i=0;i<nums.size();++i) {
                m[nums[i]]++;
            }
            vector<vector<int>> v;
            vector<int> curr;
            solve(m, m.begin(), curr, v);
            return v;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.