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

    描述
    Given a sorted array, remove the duplicates in place such that each element appear only
    once and return the new length.
    Do not allocate extra space for another array, you must do this in place with constant memory.
    For example, Given input array A = [1,1,2],
    Your function should return length = 2, and A is now [1,2].
    分析

    代码 

     1 public class solution {
     2 
     3     public static void main(String[] args) {
     4         
     5         int[] A= {1,1,1,2,2,3};
     6         int n = A.length;
     7         int index=removeDuplicates(A,n);
     8         int[] B=new int[index];//定义好数组长度不好改
     9         for(int i=0;i<index;i++) {
    10             B[i]=A[i];
    11             System.out.println(B[i]);
    12         }
    13 
    14     }
    15         public static int removeDuplicates1(int A[],int n) {
    16         if(n==0) return 0;
    17         int index=0;
    18         for(int i=1;i<n;i++) {
    19             if(A[index]!=A[i])
    20                 A[++index]=A[i];
    21         }
    22         return index+1;   //返新的长度
    23     }
    24 }
  • 相关阅读:
    Linux各目录的意义
    LinuxVIM编辑器用法
    Linux自动同步时间
    bash的基本特性
    shell-homeworkone
    shell
    笔记
    Python-1-Day
    Linux使用BIND提供域名解析服务
    Linuxautofs自动挂载服务
  • 原文地址:https://www.cnblogs.com/ncznx/p/9167232.html
Copyright © 2011-2022 走看看