Remove Element
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.
My Submitted Code
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int start=0;
for(int i=0 ;i< n;i++){
if(A[i]!=elem){
A[start++]=A[i];
}
}
return start;
}
};
本文介绍了一种在数组中移除指定值的所有实例的方法,并提供了一个C++实现示例。该算法通过一次遍历的方式将不等于指定值的元素前移,返回新的有效长度。
325

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



