My 12ms accepted code


  • 0
    P
    class Solution {
    public:
        vector<vector<int>> subsetsWithDup(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            n=nums.size();
            vector<int> vec;
            getSubset(nums,vec,0);
            return res;
        }
        void getSubset(vector<int> &nums, vector<int> vec, int i)
        {
            if(i==n)
            {
                res.push_back(vec);
                return;
            }
            int j=i+1;
            while(j<n&&nums[j]==nums[i])
                j++;
            int k=j-i;
            getSubset(nums,vec,j);
            for(int m=0;m<k;m++)
            {
                vec.push_back(nums[i]);
                getSubset(nums,vec,j);
            }
        }
    private:
        vector<vector<int>> res;
        int n;
    };

Log in to reply
 

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