A c++ solution


  • 0
    W
    int removeDuplicates(vector<int>& nums) {
            int c=0, size=nums.size(), ans=0;
            if(size==0) return 0;
            
            nums[c++]=nums[0];
            for(int i=1; i<size; ++i){
                if(nums[i]==nums[i-1]){
                    ans--;
                }else{
                    nums[c++]=nums[i];
                }
            }
            
            nums.erase(nums.begin() + c, nums.end());
            
            return nums.size();
        }

Log in to reply
 

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