Remove Duplicates from Sorted Array II

class Solution {
public:
    int removeDuplicates(int A[], int n) {
         int temp = A[0],j = 0,count;
        for(int i = 0 ; i < n ;){
            count = 0;
            while(i<n&&A[i] == temp) {i++;count++;}
            if(count>=2) A[j++] = temp;
            A[j++] = temp;
            temp = A[i];
        }
        return j;
    }
};

 

posted @ 2015-03-29 21:44  SprayT  阅读(60)  评论(0编辑  收藏  举报