My 7 line C++ solution, 60ms


  • 2
    R
    int removeDuplicates(vector<int>& nums) {
        if(nums.size() <= 1) return nums.size();
    
        vector<int>::iterator it1,it2;
        for(it1=nums.begin(),it2=nums.begin()+1; it2 != nums.end();) {
            if(*it2 == *it1) it2=nums.erase(it2);
            else {it1++;it2++;}
        }
    
        return nums.size();
    }

  • -2
    D
    This post is deleted!

  • 0
    B

    nums.erase will trigger memory re-allocation, shifting your array. You should try to write this without calling std::vector::erase().


Log in to reply
 

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