zoukankan      html  css  js  c++  java
  • leetcodeRemove Duplicates from Sorted Array(easy) /java

    我决定先刷easy。

    这道题的诡异之处在于,不仅你得输出长度,你还得更改nums[]数组,把冗余的数清掉。比如

    import java.io.*;
    import java.util.*;
    
    public class Solution {
        public static int removeDuplicates(int[] nums) {
            int r=0;
            int len=nums.length;
            int i=0,j=0;
            if(len<2)
                return 0;
            int cmp=nums[0];
            int c=0;
            nums[j]=nums[0];
            j++;
            for(i=1;i<len;i++)
            {
                if(cmp!=nums[i])
                {
                    cmp=nums[i];
                    nums[j]=nums[i];
                    j++;
                }
                else
                {
                    c++;
                }
            }
            r=len-c;
            return r;
        }
        public static void main(String[] args)
        {
            int[] a={1,1,1,1,4,5};
            System.out.println(removeDuplicates(a));
            int[] b={1,1,1,5};
            System.out.println(removeDuplicates(b));
        }
    }

    还算比较简单的一道题。

  • 相关阅读:
    Sum of Medians
    MINSUB
    Girls Love 233
    How Many Answers Are Wrong
    Sorting It All Out
    Cube Stacking
    Boolean Expressions
    表达式求值
    #1301 : 筑地市场
    用seaborn对数据可视化
  • 原文地址:https://www.cnblogs.com/zhenzhenhuang/p/7109557.html
Copyright © 2011-2022 走看看