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 p=0;
int q=0;
while(q<n)
{
if(A[q]!=elem)
{
A[p]=A[q];
++p;
}
++q;
}
return p;
}
本文介绍了一种在数组中移除特定值并返回新长度的算法实现,采用双指针技术,通过遍历数组来过滤指定元素,并保持其余元素的相对顺序。
166

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



