[Leetcode] 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.

Solution:

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

 

posted @ 2014-10-26 12:20  Phoebe815  阅读(102)  评论(0编辑  收藏  举报