2023每日刷题(四十七)
Leetcode—704.二分查找
实现代码
int lower_bound(int* arr, int numsSize, int tar) {
int left = 0, right = numsSize;
int mid = left + (right - left) / 2;
while(left < right) {
mid = left + (right - left) / 2;
if(arr[mid] < tar) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
int search(int* nums, int numsSize, int target) {
int res = lower_bound(nums, numsSize, target);
if(res < numsSize) {
if(nums[res] == target) {
return res;
}
}
return -1;
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!