谢谢大神
发布网友
发布时间:2022-06-02 11:48
我来回答
共1个回答
热心网友
时间:2023-10-18 08:32
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class arrlist
{
public:
arrlist()
{
shuzu.push_back(0xdeadbeef);
}
//O(lgN)
int listsearch(int x)
{
//二分查找
int s = 1;
int e = shuzu.size();
while(s!=e)
{
int mid = s+(e-s)/2;
if(shuzu[mid]==x)
return mid;
else if(shuzu[mid]>x)
e = mid -1;
else
s = mid+1;
}
return 0;
}
//O(lgN)
int listdelete(int x)
{
int s = 1;
int e = shuzu.size();
while(s!=e)
{
int mid = s+(e-s)/2;
if(shuzu[mid]==x)
{
int i= mid;
int j= mid;
while(i>=1 && shuzu[i]==x)
i--;
i++;
while(j<shuzu.size() && shuzu[j]==x)
j++;
shuzu.erase(i+shuzu.begin(),j+shuzu.begin());
break;
}
else if(shuzu[mid]>x)
e = mid -1;
else
s = mid+1;
}
return 0;
}
//追问才行