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.
一个快慢指针即可,还有一种情况无给定的元素时刻优化。
int removeElement(int A[], int n, int elem)
{
if(n <= 0 || A == NULL) return 0;
int low = 0;
int fast = 0;
while(fast < n)
{
if(A[fast] == elem)
{
fast++;
}
else
{
if(low != fast)//无给定的元素时刻优化
{
A[low] = A[fast];
}
low++;
fast++;
}
}
return low;
}