class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = 0;
for (int i = 0; i < n; ++i)
{
if (A[i] != elem)
A[len++] = A[i];
}
return len;
}
};
second time
class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int curIdx = 0;
for(int i = 0; i < n; ++i)
{
if(A[i] != elem) A[curIdx++] = A[i];
}
return curIdx;
}
};
本文提供了一种使用C++实现的从数组中移除指定元素的方法。通过遍历数组并将非目标元素复制到新的位置,该算法有效地压缩了数组,排除了指定的元素。
1445

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



