Remove Duplicates from Sorted Array

public class Solution {
    public int removeDuplicates(int[] A) {
        if(A==null || A.length==0) return 0;
        // 巧妙的做法http://www.cnblogs.com/springfor/p/3871038.html
        int len =0; // 记录最后一个不重复的点在新数组中位置,尾端
        for(int i=1; i<A.length;i++){
            if(A[i]!=A[i-1]){
                if(A[i]!=A[len]){
                    A[len+1] = A[i];
                    len++;
                }
            }
        }
        return len+1;
    }
}

 

posted @ 2015-04-10 12:59  世界到处都是小星星  阅读(76)  评论(0编辑  收藏  举报