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) {
int cnt = 0;
for (int i = 0; i < n - cnt; ++i) {
if (elem == A[i]) {
A[i] = A[n- 1 - cnt];
cnt++;
i--;
}
}
return n - cnt;
}
时间: 2024-10-05 03:02:10