zoukankan      html  css  js  c++  java
  • Remove Element leetcode java

    题目:

    Given an array and a value, remove all instances of that value in place and return the new length.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    题解:

     这道题跟remove duplicate sorted array是一样的。。。还是双指针,一个帮忙记录length外加替换不是elem的值,一个帮忙往前找。

    代码如下:

     1     public int removeElement(int[] A, int elem) {
     2         if(A==null||A.length == 0)
     3             return 0;
     4             
     5         int len = 0;
     6         for(int i =0; i < A.length;i++){
     7             if(A[i] != elem){
     8                 if(A[len]!=A[i])
     9                     A[len]=A[i];
    10                 len++;
    11             }
    12         }
    13         return len;
    14     }

  • 相关阅读:
    Kafka与Flume之集成比较
    Kafka之配置信息
    Kafka 之 Streams
    Kafka 之producer拦截器(interceptor)
    MySql(一)
    Android学习(一)
    Android学习(二)
    Android学习(三)
    Andoroid学习(四)
    Android学习(五)
  • 原文地址:https://www.cnblogs.com/springfor/p/3871046.html
Copyright © 2011-2022 走看看