Sharing my 20ms C++ solution


  • 0
    T
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            int n = nums.size();
            if(n<3)
                return n;
            int index = 2;
            for(int i=2; i<n; i++)
            {
                if(nums[i] != nums[index-1] || nums[i] != nums[index-2])
                {
                    nums[index] = nums[i];
                    index++;
                }
            }
            
            return index;
        }
    };

Log in to reply
 

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