Remove Duplicates from Sorted Array II

 

void  swap(int* a, int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        int cnt = 0;
        int cur = -1,i;
        for(i=0;i<n;i++)
        {
            if(cur==-1||A[cur]!=A[i])
            {
                cnt = 1;
                ++cur;
                swap(&A[cur],&A[i]);
            }else if(cnt==1)
            {
                cnt++;
                ++cur;
                swap(&A[cur],&A[i]);
            }
        }
        return cur+1;
        
    }

  

posted @ 2013-10-05 16:47  summer_zhou  阅读(120)  评论(0编辑  收藏  举报