Share my C++ code with std iterator.


  • 0
    C

    Since use std::unique is kind of cheater. Here is my code of C++ by means of std iterator.

    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            if(nums.size()<=1) return nums.size();
            auto targetPos = nums.begin();
            auto currentPos = nums.begin()+1;
            while(currentPos!=nums.end()){
                if(*currentPos == *targetPos){
                    ++currentPos;
                }else{
                    ++targetPos;
                    *targetPos = *currentPos;
                    ++currentPos;
                }
            }
            return targetPos-nums.begin()+1;
        }
    };

Log in to reply
 

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