zoukankan      html  css  js  c++  java
  • 26. Remove Duplicates from Sorted Array

        /*
         * 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;
        }
  • 相关阅读:
    排序
    最小栈
    移除链表元素
    回文链表
    maven自动建立目录骨架
    maven的结构和构建命令
    递归
    链表的中间结点
    括号匹配
    软件工程个人作业01
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5400225.html
Copyright © 2011-2022 走看看