本子君

二分模板和区别

 模板一

int[] nums = {0, 0, 1, 2, 2, 5, 6};
int l = 0, r = 6, target = 0;
while(l < r)
{
    int mid = l + r >> 1;
    if(nums[mid] >= target) r = mid;
    else l = mid + 1;
}
return l;

//返回结果是0

 

 模板二

int[] nums = {0, 0, 1, 2, 2, 5, 6};
int l = 0, r = 6, target = 0;
while(l < r)
{
    int mid = l + r + 1>> 1;
    if(nums[mid] <= target) l = mid;
    else r = mid - 1;
}
return l;

//返回结果是1

 

posted on 2021-04-07 21:37  本子君  阅读(65)  评论(0编辑  收藏  举报

导航