Add elements 1 by 1 and check whether adjacent elements are same


  • 0
    class Solution {
    public:
        vector<vector<int>> subsetsWithDup(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            vector<vector<int>> subs(1,vector<int>());
            int cursize,dpsize=1;
            for(int i=0;i<nums.size();i++){
                cursize=subs.size();
                if(i>0&&nums[i]!=nums[i-1])dpsize=cursize;
                for(int j=cursize-dpsize;j<cursize;j++){
                    subs.push_back(subs[j]);
                    subs.back().push_back(nums[i]);
                }
            }
            return subs;
        }
    };

Log in to reply
 

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