Remove Element

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.

 1 public class Solution {
 2     public int removeElement(int[] A, int elem) {
 3         int p1 = 0;
 4         int p2 = 0;
 5         while(p1<A.length){
 6             if(elem==A[p1]){
 7                 p1++;
 8             }
 9             else{
10                 A[p2] =A[p1]; 
11                 p2++;p1++;
12             }
13         }
14         return p2;
15     }
16 }
View Code

 

posted @ 2014-02-06 14:00  krunning  阅读(79)  评论(0编辑  收藏  举报