class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int count=0;
for(int i=0;i<=n-1;i++)//先确定A[]中有无elem
if(A[i]==elem)
count++;
if(count==0)//A[]中无elem
return n;
else//A[]中有elem
{//利用快速排序中partition的交换的思想
int sta=0,end=n-1;
while(sta<end)
{
while(A[sta]!=elem)
sta++;
while(A[end]==elem)
end--;
if(sta<end)
{
int tmp=A[sta];
A[sta]=A[end];
A[end]=tmp;
}
}
return sta;
}
}
};
【leetcode】Remove Element
最新推荐文章于 2020-12-07 18:02:23 发布
本文介绍了一个名为classSolution的类,其中包含一个名为removeElement的方法,用于从整数数组A中移除所有等于elem的元素,并返回新数组的长度。通过快速排序中的partition思想实现此功能。

1426

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



