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.
题意是从数组中删除所有指定的元素并返回最后长度, 直接用一个变量记录将要被覆盖的数字的位置即可
class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if (n == 0 ) return 0;
int len = n;
for (int i = 0, pos = 0; i < n; ++i)
{
if (A[i] == elem)
--len;
else
A[pos++] = A[i];
}
return len;
}
};