当前位置:网站首页>Binary search basis
Binary search basis
2022-07-05 05:16:00 【lee2813】
One 、 Two points search
First , It is clear that the premise of binary search is for ordered arrays , Then the significance of binary search on this basis is to use the order of data structure to simplify the time and space overhead of the algorithm .
in addition , Binary search is the same as double pointer traversal array , But the difference is that binary search moves every time ‘ Half interval ’ length , The double pointer problem moves step by step .
Two 、 classification
The standard of classification here is the method to solve the problem :
- The interval is defined as left closed and right closed , The corresponding boundary condition is
l<=r
, Because when there is only one number left , Still reasonable , for example[5,5]
, At this time, it is written as :
int l = 0;
int r = nums.size()-1;
while(l<=r){
mid = (l+r)/2;
...
}
- The interval is defined as left closed and right open , The corresponding boundary condition is
l<r
, Because when there is only one number left , unreasonable , for example[5,5)
, and[5,6)
reasonable , At this time, it is written as :
int l = 0;
int r = nums.size();
while(l<r){
mid = (l+r)/2;
...
}
Both can solve the problem , But there are some differences in writing , It is suggested to learn one .
边栏推荐
- Stm32cubemx (8): RTC and RTC wake-up interrupt
- Quick sort summary
- Unity enables mobile phone vibration
- Development error notes
- Solon 框架如何方便获取每个请求的响应时间?
- Learning notes of "hands on learning in depth"
- Unity parallax infinite scrolling background
- Listview is added and deleted at the index
- Collapse of adjacent vertical outer margins
- Download and use of font icons
猜你喜欢
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
Unity check whether the two objects have obstacles by ray
C language Essay 1
Fragment addition failed error lookup
Web APIs DOM节点
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
嵌入式数据库开发编程(零)
Grail layout and double wing layout
Research on the value of background repeat of background tiling
用 Jmeter 工具做个小型压力测试
随机推荐
Research on the value of background repeat of background tiling
Unity sends messages and blocks indecent words
[turn]: Apache Felix framework configuration properties
Solon Logging 插件的添加器级别控制和日志器的级别控制
Basic knowledge points of dictionary
Lua GBK and UTF8 turn to each other
3dsmax common commands
[LeetCode] 整数反转【7】
How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
[转]MySQL操作实战(一):关键字 & 函数
BUUCTF MISC
Embedded database development programming (VI) -- C API
2022/7/1 learning summary
cocos_ Lua listview loads too much data
Fragment addition failed error lookup
远程升级怕截胡?详解FOTA安全升级
Unity find the coordinates of a point on the circle
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Cocos progress bar progresstimer
The next key of win generates the timestamp file of the current day