Remove Duplicates from Sorted Array II

Question:

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.

Solution:

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4             int n=nums.size();
 5     if(n<=2) return n;
 6     int i=2;
 7     for(int j=2;j<n;j++)
 8     {
 9         if(nums[j]!=nums[i-2])
10         {
11             nums[i]=nums[j];
12             i++;
13 
14         }
15     }
16     return i;
17         
18     }
19 };

原文地址:https://www.cnblogs.com/riden/p/4631347.html