Leetcode: Remove Duplicates from Sorted Array II

class Solution {
public:
    int removeDuplicates(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (n <= 2) return n;

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

        return cur + 1;
    }
};

 

posted @ 2013-05-01 21:41  caijinlong  阅读(112)  评论(0编辑  收藏  举报