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

    Remove Duplicates from Sorted Array

    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     public int removeDuplicates(int[] A) {
     3         if(A.length == 0)
     4             return A.length;
     5         int B[] = new int[A.length];
     6         int k = 1;
     7         B[0] = A[0];
     8         for(int i = 1; i< A.length; i++){
     9             if(A[i] != B[k - 1]){
    10                 B[k++] = A[i];
    11             }
    12         }
    13         System.arraycopy(B, 0, A, 0, k);
    14         
    15         return k;
    16     }
    17 }
  • 相关阅读:
    requirejs 第一个实例
    ionic + cordova 环境搭建
    免安装mysql配置
    ConcurrentHashMap
    volatile和synchronized
    zookeeper集群安装
    题目
    Nginx
    CountDownLatch
    自己总结
  • 原文地址:https://www.cnblogs.com/luckygxf/p/4088956.html
Copyright © 2011-2022 走看看