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.
分析:遍历老数组,i作为老数组的下标,j作为新数组的下标,遇到指定值,数组长度减一,否则将老数组当前值存到新数组的下标所指位置
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int len = n;
int i = 0;
int j = 0;
for(i = 0; i < n; i++)
{
if (A[i] == elem)
len--;
else
A[j++] = A[i];
}
return len;
}
};