代码改变世界

leetcode - Remove Duplicates from Sorted Array II

2013-11-02 21:20  张汉生  阅读(174)  评论(0)    收藏  举报

 

class Solution {
public:
    int removeDuplicates(int A[], int n) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if (n<=1)
            return n;
        int last = 1;
        for (int i=1; i<n; i++){
            if (last<2 || A[last-1] != A[i] || A[last-2] != A[i])
                A[last++] = A[i];
        }
        return last;
    }
};