首页 > 编程语言 > 详细

Remove Element leetcode java

时间:2014-07-27 21:28:05      阅读:278      评论:0      收藏:0      [点我收藏+]

题目:

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     }

Remove Element leetcode java

原文:http://www.cnblogs.com/springfor/p/3871046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!