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-03-30 14:51:54 发布