四种二分
作者:
809
,
2021-07-20 22:20:17
,
所有人可见
,
阅读 236
while(l < r)
{
int mid = l + r >> 1;
if(up[mid] < target)
l = mid + 1;
else
r = mid;
}
//递增序列找大等target
//多个target得到下标最小
while(l < r)
{
int mid = l + r + 1 >> 1;
if(up[mid] <= target)
l = mid;
else
r = mid - 1;
}
//递增序列找小等target
//多个target得到下标最大
while(l < r)
{
int mid = l + r >> 1;
if(down[mid] > target)
l = mid + 1;
else
r = mid;
}
//递减序列找小等target
//多个target得到下标最小
while(l < r)
{
int mid = l + r + 1 >> 1;
if(down[mid] >= target)
l = mid;
else
r = mid - 1;
}
//递减序列找大等target
//多个target得到下标最大