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.
Tag:双指针,数组的原地操作。
#include <iostream>
using namespace std;
int removeElement(int A[], int n, int elem)
{
if(A == NULL || n == 0)
return 0;
int i = 0;
int j = 0;
while(1)
{
if(A[j] != elem)
{
A[i++] = A[j++];
}
else j++;
if(j >= n)
break;
}
return i;
}
int main()
{
int num[] = {1, 1, 2};
int n = removeElement(num, 3, 3);
for(int i = 0; i < n; i++)
cout<<num[i]<<" ";
}