9ms, beat 82%


  • 0
    Z
    class Solution {
    public:
        vector<vector<int>> subsetsWithDup(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            vector<vector<int>> res;
            vector<int> ni;
            res.push_back(ni);
            
            for(int i=0;i<nums.size();++i)
            {
                vector<int> v;
                v.push_back(nums[i]);
                res.push_back(v);
                subsetsWithDup(res,nums,v,i+1);
                while(i < nums.size() && v[0] == nums[i]) ++i;
                --i;
            }
            return res;
        }
    private:
        void subsetsWithDup(vector<vector<int>>& res,vector<int>& nums,vector<int>& v,int cur)
        {
            for(int i=cur;i<nums.size();++i)
            {
                int tmp = nums[i];
                v.push_back(tmp);
                res.push_back(v);
                subsetsWithDup(res,nums,v,i+1);
                v.pop_back();
                while(i < nums.size() && tmp == nums[i]) ++i;
                --i;
            }
        }
    };

Log in to reply
 

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