20ms c++


  • 0
    B
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            if (nums.size()<3) return nums.size();
            vector<int> ans;
            ans.push_back(nums[0]);
            ans.push_back(nums[1]);
            for(int i=2;i<nums.size();i++)
            {
                if (nums[i]!=nums[i-2])
                    ans.push_back(nums[i]);
            }
            nums.clear();
            nums.resize(ans.size());
            copy(ans.begin(),ans.end(),nums.begin());
            return nums.size();
        }
    };
    

Log in to reply
 

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