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 }
  • 相关阅读:
    Junit连接oracle数据库
    java判断字符串是否由数字组成
    Hibernate各种主键生成策略与配置详解
    一对多映射关系
    one-to-one 一对一映射关系(转 wq群)
    工厂模式
    struts2
    创建JUtil
    jdbc
    压缩数据
  • 原文地址:https://www.cnblogs.com/ncznx/p/9167232.html
Copyright © 2011-2022 走看看