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; } }
浙公网安备 33010602011771号