题目
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) {
int i=0;
for(int j=0;j<n;j++)
{
if(A[j]!=elem)
A[i++]=A[j];
}
return i;
}
};
本文介绍了一种在数组中高效移除特定值的方法,并提供了一个C++实现示例。该算法通过一次遍历的方式将目标值从数组中移除,并返回新的有效长度。

被折叠的 条评论
为什么被折叠?



