码迷,mamicode.com
首页 > 其他好文 > 详细

26. Remove Duplicates from Sorted Array

时间:2016-04-17 07:59:24      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 26. Remove Duplicates from Sorted Array
     * 12.4 by Mingyang
     */
     public int removeDuplicates1(int[] nums) {
            int len=nums.length;
            if(nums==null||len==0)
              return 0;
            if(len==1)
              return 1;
              int start=0;
            for(int i=1;i<len;i++){
              if(nums[i]==nums[start]){
                  continue;
              }else{
                  start++;
                  nums[start]=nums[i];
              }
            }
            return start+1;
        }
     /*
      * 下面就是别人的代码,接下来就说说如何写出漂亮简洁的代码
      * 首先,不用判定为1,因为为1的话在for循环根本都不需要执行
      * 然后不需要判断A[i]==A[j],因为相等continue还不如直接忽略掉那个部分
      */
    public int removeDuplicates(int[] A) {
        if (A.length==0) return 0;
        int j=0;
        for (int i=1; i<A.length; i++)
            if (A[i]!=A[j])
                A[++j]=A[i];
        return ++j;
    }

 

26. Remove Duplicates from Sorted Array

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5400225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!