[LintCode/LeetCode] Remove Element [Two Pointers]

NO IMAGE

Problem

Given an array and a value, remove all occurrences of that value in place and return the new length.

The order of elements can be changed, and the elements after the new length don’t matter.

Example

Given an array [0,4,4,0,0,2,4,4]

2. 雙指標II

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