当前位置:网站首页>二分查找的理解
二分查找的理解
2022-06-12 17:15:00 【老油条666】
常见的二分模板:
模板一,区间是左闭右闭的,即查找区间是[l,r]
int binary_search(int l, int r, int a[], int x) //返回值是元素x在数组a中的位置(下标)
{
int mid = 0;
while(l <= r)
{
mid = (l+r) >> 1;
if(a[mid] == x)
return mid;
if(a[mid] < x)
l = mid+1;
else
r = mid-1;
}
return -1;
}模板二,区间是左闭右开的,即查找区间是[l,r),老外喜欢玩这个,然后整个STL基本都是这种左闭右开的。
int binary_search(int l, int r, int a[], int x)
{
int mid = 0;
while(l < r)
{
mid = (l+r) >> 1;
if(a[mid] == x)
return mid;
if(a[mid] < x)
l = mid+1;
else
r = mid ;
}
return -1;
}注意这两个模板的区别,如果是左闭右闭的,while循环处应该是可以l == r的,然后在更新r的时候要将r赋值为mid-1,因为查找子区间也应该是左闭右闭的区间(因为mid在本次查找的时候已经查过了,所以子区间划分的时候mid不应该在区间中)。
如果是左闭右开的模板,则在更新的时候r要赋值为mid,这样才能保证查找子区间的时候也是左闭右开的。
l更新的时候始终是mid+1,毕竟两个模板都是左闭区间。
边栏推荐
- Flink 维表异步查询的实现以及问题排查
- R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置ylab参数设置Y轴的轴标签文本(y axis)
- bug记录:更新数据库时报错:Data truncation: Incorrect datetime value:
- R语言使用pdf函数将可视化图像结果保存到pdf文件中、使用pdf函数打开图像设备、使用dev.off函数关闭图像设备、自定义width参数和height参数指定图像的宽度和高度
- R language uses ggplot2 to visualize the density graph (curve) of specific data columns in dataframe data, and uses Xlim parameter to specify the range of X axis
- Selenium element positioning
- [BSP video tutorial] stm32h7 video tutorial Issue 8: the last issue of the MDK theme, the new generation of debugging technologies event recorder and RTT, and using stm32cubemx to generate project tem
- Operating with idle funds
- Installation and use of rolabelimg
- MySQL transaction introduction and transaction isolation level
猜你喜欢

rolabelImg的安装使用

Some minor problems and solutions encountered when using ubantu

两位新晋Committer的“升级攻略”

Nebula's practice of intelligent risk control in akulaku: training and deployment of graph model

First acquaintance with go language

Sizepolicy policy in layout management

Record the use of yolov5 to detect rotating targets

Swintransformer network architecture

Compilation optimization of performance optimization

Learn the mitmproxy packet capturing tool from scratch
随机推荐
Go variables
Tidb Hackathon 2021 - pcloud: conduct icloud pcloud team interview on the database
Google浏览器调试技巧
R语言使用ggplot2可视化dataframe数据中特定数据列的密度图(曲线)、并使用xlim参数指定X轴的范围
The R language uses the tablestack function of epidisplay package to generate statistical analysis tables based on grouped variables (including descriptive statistical analysis, hypothesis test, diffe
Différence entre le mode grand et le mode petit
(八)goto关键字
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
(三)Golang - 数据类型
如何查看、修改和删除SSH
Google browser debugging skills
Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code
Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现
Microsoft Office MSDT Code Execution Vulnerability (cve-2022-30190) vulnerability recurrence
(四)Golang运算符
Making nearly $90billion, Buffett's latest heavy stock exposure
Li Kou today's question 926 Flip string to monotonic increment
软件工程 学生信息管理系统 结构化的需求分析
JVM memory model and local memory
Gerrit+2触发Jenkins任务