5/30/2014

49. Remove Duplicates from Sorted Array II

public class Solution {
    public int removeDuplicates(int[] A) {
        if (A.length<=1) return A.length;
       
        int p=0;
        int q=1;
        int flag=1;
       
        while (q<A.length) {
            if (A[q]==A[p]) {
                flag++;
                if (flag<=2) {
                    p++;
                    A[p] = A[q];
                }
            } else {
                flag=1;
                p++;
                A[p]=A[q];
            }
            q++;
        }
       
        return p+1;
    }
}

1 条评论: