当前位置:网站首页>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 .
边栏推荐
猜你喜欢
The present is a gift from heaven -- a film review of the journey of the soul
质量体系建设之路的分分合合
Panel panel of UI
[turn to] MySQL operation practice (I): Keywords & functions
十年不用一次的JVM调用
BUUCTF MISC
Unity3d learning notes
Leetcode word search (backtracking method)
How to choose a panoramic camera that suits you?
[转]MySQL操作实战(三):表联结
随机推荐
Download xftp7 and xshell7 (official website)
2022/7/2做题总结
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
PMP candidates, please check the precautions for PMP examination in July
Time format conversion
一个新的微型ORM开源框架
2022年上半年国家教师资格证考试
When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
Magnifying glass effect
Listview pull-down loading function
Optimization scheme of win10 virtual machine cluster
FVP和Juno平台的Memory Layout介绍
room数据库的使用
UE 虚幻引擎,项目结构
2021-10-29
Use of snippets in vscode (code template)
服务熔断 Hystrix
Simple modal box
Cocos progress bar progresstimer