C++ 36ms use map! easy to uderstand!


  • 0
    G
    class Solution {
    public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.empty()){
            return 0;
        }
        map<int,int>map;
        int length=0;
        for(int i=0;i<nums.size();i++){
            map[nums[i]]++;
                if(map[nums[i]]>2){
                map[nums[i]]=2;
            }
        }
        for(auto mp:map){
            length+=mp.second;
            for(int i=0;i<mp.second;i++){
                nums.insert(nums.begin(),mp.first);
            }
           //cout<<mp.first<<"\n";
        }
        reverse(nums.begin(),nums.begin()+length);
        return length;
    }
    

    };


Log in to reply
 

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