26. 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.

---

Similar to 25, remove dup from a sorted array

---

public class Solution {
    public int removeElement(int[] A, int elem) {
        
        int i=0, j=0;
        while(j < A.length){
            if(A[j] == elem){
                j++;
            }else{
                if(i != j)
                    A[i] = A[j];
                i++;
                j++;
            }
        }
        return i;
        
    }
}

 

posted @ 2013-09-02 04:48  LEDYC  阅读(273)  评论(0)    收藏  举报