My easy understanding 16ms c++ solution


  • 0
    Y
    int removeDuplicates(vector<int>& nums) {
            int n = nums.size();
            if(n <= 2) return n;
            int curpos = 0;
            bool flag = false;
            for(int i = 1; i < n; i++){
                if(nums[i]!=nums[curpos]){
                    nums[++curpos] = nums[i];
                    flag = false;
                }else if(!flag){
                    nums[++curpos] = nums[i];
                    flag = true;
                }
            }
            return ++curpos;
        }

Log in to reply
 

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