Remove Duplicates from Sorted Array II

public class Solution {
    public int removeDuplicates(int[] A) {
        //http://needjobasap.blogspot.com/2014/01/remove-duplicates-from-sorted-array-ii.html
        // 我觉得这个做法还是最简洁的,我觉得我还可以抢救一下,我之前困惑是尼玛想复杂了,认为只能记录2个的,人家是“at most twice”
        if(A==null || A.length<1) return 0;
        int ind =1, cnt = 1;
        for(int i =1; i<A.length; i++){
            if(A[i]==A[i-1] ){
                cnt++;
            }else
                cnt=1;
            
            if(cnt<=2) { // 所以如果是一个元素,就喜洋洋的挪ind啦
            A[ind] = A[i];
            ind++;
            }
        }
        return ind;
    }
}

 

posted @ 2015-04-17 07:06  世界到处都是小星星  阅读(67)  评论(0编辑  收藏  举报