首页 > 其他 > 详细

Remove Duplicates from Sorted Array II

时间:2015-04-17 08:22:08      阅读:262      评论:0      收藏:0      [点我收藏+]
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;
    }
}

 

Remove Duplicates from Sorted Array II

原文:http://www.cnblogs.com/jiajiaxingxing/p/4433874.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!