解析:
本题是交互题,所以题目已经把接口准备好了,只需要我们返回就行了
所以一开始没有把关系给你,是通过compare 返回的类型来确定
我们采用二分插入排序,二分已经插入的元素的位置,判断在哪个位置上。
然后通过数组移位,使其移动到二分的位置
// Forward declaration of compare API.
// bool compare(int a, int b);
// return bool means whether a is less than b.
class Solution {
public:
vector<int> specialSort(int N) {
vector<int> res;
res.push_back(1);
for(int i=2;i<=N;i++)
{
int l=0;
int r=res.size()-1;
while(l<=r)
{
int mid=l+r>>1;
if(compare(res[mid],i)) l=mid+1;
else r=mid-1;
}
res.push_back(i);
for(int j=res.size()-2;j>r;j--) swap(res[j],res[j+1]);
}
return res;
}
};