当前位置:网站首页>Binary search (search interval is left closed and right open)
Binary search (search interval is left closed and right open)
2022-08-07 08:40:00 【HwWwWwK】
二分查找(The search range is left closed and right open)
文章目录
0. 参考链接
1. 思考
I think binary search is easy to figure outbug的算法,Because its overall idea is very simple,But it's too much about the details.
The more troublesome problems are as follows
假设lis the left boundary of the search interval,ris the right boundary of the search interval,mis the rounded down median, A为有序表
循环条件: l < r 还是 l <= r ?
边界调整: l = m 还是 l = m + 1; r = m 还是 r = m - 1 ?
返回结果: return 的结果是{l,l+1,r,r+1} ?
The above problems can be solved by taking a search interval that is closed on the left and open on the right,最终结论如下
- 循环条件: l < r (The search range is not empty)
- 边界调整: l = m + 1, r = m
- 返回结果: return l 或 return r 均可
personal perception
It is because of the binary divisionThere are two ways to take the median的问题,Left closed right open directly biased selectionThe median rounded down
m = (l + r) / 2 而不是 m = (l + r + 2) / 2,to reduce discussion.
C++STLThe binary search function implemented by the library,The parameters provided are the same idea
lower_bound(first,last,value):从下标[first, last)内,二分查找第一个大于或等于value的数字
upper_bound(first,last,value):从下标[first, last)内,二分查找第一个大于value的数字
2. lower_boundThe same effect achieves the idea
// 求 第一个 x >= v
while (l < r) {
m = l + ((r - l) >> 1); // Spill proof
if (A[m] < v) l = m + 1;
else r = m;
}
return l; // 或 return r;
2.1 循环条件
The condition under which the loop continues during the search
- [l,r)不为空,需要继续搜索
- 此时[l0,l)所有元素(若存在)都<v
- 此时[r,r0)所有元素(若存在)都>=v
2.2 边界调整
A[m]<v,那么m在[l0,l)区间内 应调整 m < l,则 l = m + 1
A[m]>=v,那么m在[r,r0)区间内 应调整 m >= r,则 r = m
2.3 返回结果
最终循环结束时
[l,r)为空, l = r
[l0,l)所有元素(若存在)都<v,
[r,r0)所有元素(若存在)都>=v,rFor the desired lower bound
3. upper_boundThe same effect achieves the idea
// 求 第一个 x > v
while (l < r) {
m = l + ((r - l) >> 1);
if (A[m] <= v) l = m + 1; // The difference is here,A[m] == v 的归属
else r = m;
}
return l; // 或 return r;
3.1 循环条件
The condition under which the loop continues during the search
- [l,r)不为空,需要继续搜索
- 此时[l0,l)所有元素(若存在)都<=v
- 此时[r,r0)所有元素(若存在)都>v
3.2 边界调整
A[m]<=v,那么m在[l0,l)区间内 应调整 m < l,则 l = m + 1
A[m]>v,那么m在[r,r0)区间内 应调整 m >= r,则 r = m
3.3 返回结果
最终循环结束时
[l,r)为空, l = r
[l0,l)所有元素(若存在)都<=v,
[r,r0)所有元素(若存在)都>v,rFor the desired lower bound
4. 其他
可以用lower_bound和upper_boundSolve the common binary search problem as follows
- 小于v的上界 lower_bound(l,r,v) - 1
- 小于等于v的上界 upper_bound(l,r,v) - 1
- 大于等于v的下界 lower_bound(l,r,v)
- 大于v的下界 upper_bound(l,r,v)
The middle two are more common
5. 例
int searchInsert(int* nums, int numsSize, int target)
{
int l, r;
l = 0, r = numsSize;
while (l < r) {
int m = l + ((r - l) >> 1);
if (nums[m] < target) l = m + 1;
else r = m;
}
return l;
}
LettCode 34.在排序数组中查找元素的第一个和最后一个位置
int* searchRange(int* nums, int numsSize, int target, int* returnSize)
{
int l = 0, r = numsSize, m;
int* ret = malloc(sizeof(int) * 2);
*returnSize = 2;
ret[0] = ret[1] = -1;
if (numsSize == 0) return ret;
while(l < r) {
m = l + ((r - l) >> 1);
if (nums[m] < target) l = m + 1;
else r = m;
}
if (r != numsSize && nums[l] == target) ret[0] = l;
l = 0, r = numsSize;
while (l < r) {
m = l + ((r - l) >> 1);
if (nums[m] <= target) l = m + 1;
else r = m;
}
if (l != 0 && nums[l - 1] == target) ret[1] = l - 1;
return ret;
}
边栏推荐
猜你喜欢

pip3升级后报错f“pip[sys.version_info.major)“

31. Understand what is avalanche, penetration and breakdown of redis?What happens after redis crashes?What are the countermeasures

Unity 3D 游戏通用系统设置页面,自定义按键设置,背景虚化,图像设置,亮度对比度饱和度音量调节,分辨率窗口化,帧率垂直同步,抗锯齿,阴影质量,纹理质量设置
![[Promise] Promise use / callback hell problem async-await / macro queue and micro queue](/img/59/8ebdd70122b7e3c094f971454a5336.png)
[Promise] Promise use / callback hell problem async-await / macro queue and micro queue

2022 TV cup mathematical modeling - online documentation

canvas图像绘制(有放大缩小和拖动功能)

The network layer first

双向链表的增删查改

5 Crypto Themes Poised to Lead the Next Bull Market

MES生产管理系统是什么?有ERP系统了为什么还要上
随机推荐
The network layer first
multiplexing technology
Scala——While和do..While循环控制
ABP 6.0.0-rc.1的新特性
排序--选择排序
选择排序(简单选择排序和堆排序)
Heavy Live | ORB-SLAM3 Series Code Explanation: Key Frames Topic 1
Redis principle and way of source data persistence RDB introduction and source code parsing
DeFi Prospects: An Overview of Q2 Progress of Mainstream DeFi Protocols
Recursion: Understanding and Applying
HPC Technology: Analysis of MPICH Implementation Principle
Model fine-tuning transfer learning Finetune method Daquan
哈希-闭散列
架构师杂谈【摘抄】
力扣:494. 目标和
10 things he learned after teaching most of his life at MIT
Sort -- bubble sort
Addition, deletion, search and modification of doubly linked list
redis的原理和源码-事件机制的介绍和源码解析(eventloop、fileevent、timeevent)
redis的原理和源码-redis的六种数据类型基本介绍:string、hash、list、set、zset、stream