Leetcode-Remove Duplicates II

http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

题目描述

  • Follow up for "Remove Duplicates":
  • What if duplicates are allowed at most twice?
  • For example,
  • Given sorted array A = [1,1,1,2,2,3],
  • Your function should return length = 5, and A is now [1,1,2,2,3].

思路

AC代码

#include <iostream>
#include <cstdio>

using namespace std;

int removeDuplicates(int A[],int n)
{
        if(n <= 2)
            return n;
        int num = 2;
        for(int i = num; i < n; i++)
        {
            if(!(A[i] == A[num - 1] && A[i] == A[num -2]))
            {
                A[num] = A[i];
                num++;
            }
        }
        return num;
}

int main()
{
        int n;
        cin>>n;
        int A[n];
        for(int i = 0; i < n; i++)
            cin>>A[i];
        cout<<removeDuplicates(A,n)<<endl;
        return 0;
}