Use Iterator.


  • 0
    N
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            vector<int>::iterator iter;
            for(iter=nums.begin();iter!=nums.end();)
            {
                if(iter+1!=nums.end()&&*(iter+1)==*iter)iter=nums.erase(iter);
                else iter++;
            }
            return nums.size();
        }
    };

Log in to reply
 

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