当前位置:网站首页>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 .
边栏推荐
- Unity sends messages and blocks indecent words
- This article is good
- Research on the value of background repeat of background tiling
- Unity synergy
- Do a small pressure test with JMeter tool
- Recherche de mots pour leetcode (solution rétrospective)
- [turn]: Apache Felix framework configuration properties
- Animation
- [LeetCode] 整数反转【7】
- 2022/7/1學習總結
猜你喜欢
随机推荐
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
Solon Logging 插件的添加器级别控制和日志器的级别控制
Redis has four methods for checking big keys, which are necessary for optimization
[轉]: OSGI規範 深入淺出
Unity connects to the database
How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
Embedded database development programming (VI) -- C API
Unity card flipping effect
Shell Sort
2022/7/1學習總結
Bubble sort summary
Listview is added and deleted at the index
Grail layout and double wing layout
The next key of win generates the timestamp file of the current day
对象的序列化
Download and use of font icons
Lua wechat avatar URL
C iterator
xftp7与xshell7下载(官网)
Collapse of adjacent vertical outer margins