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.
解题思路:
题目大意,删除数组中的指定元素。实现方法比较简单,通过指针,可以在O(n)时间复杂度内解决。
Code:
int removeElement(int A[], int n, int elem) {
int pElem=0;
for(int i=0;i<n;i++)
if(elem!=A[i])
{
A[pElem++]=A[i];
}
return pElem;
}
本文介绍了一种在O(n)时间复杂度内删除数组中指定元素的方法,通过使用指针实现,无需改变元素顺序。
433

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



