当前位置:网站首页>Binary search method
Binary search method
2022-07-24 10:33:00 【Carefully programmed purple shirt Dragon King】
introduction .
The premise of the topic is that the array is an ordered array , At the same time, the title also emphasizes There are no duplicate elements in the array , Because once there's a repeating element , The element subscript returned by binary search method may not be unique , These are the prerequisites for using dichotomy , When you see that the Title Description meets the above conditions , But think about whether you can use dichotomy . Binary search involves many boundary conditions , The logic is simple , But I just can't write well . For example, what is it while(left < right) still while(left <= right), What is the right = middle Well , Still right = middle - 1 Well ? People often write dichotomy in disorder , Mainly because The definition of interval is not clear , The definition of interval is invariant . In the process of binary search , Keep invariants , Is in the while Every boundary treatment in the search must adhere to the operation according to the definition of interval , This is it. Loop invariants The rules .
One . Determine the interval — Left and right closed or Left closed right away
Adhere to loop invariants , Keep the interval rules consistent
Two . Analysis of the specific situation
1. Left closed right closed binary search
left=0
right=len(nums)-1
while (left <= right):
middle = (left + right) //2
if (nums[middle] > target):
right=middle-1
elif (nums[middle] < target):
left=middle+1
else:
return middle
return -12. Left close right open binary search
left=0
right=len(nums)-1
while (left < right):
middle = (left + right) //2
if (nums[middle] > target):
right=middle
elif (nums[middle] < target):
left=middle+1
else:
return middle
return -13. Left open right close binary search
left=0
right=len(nums)-1
while (left < right):
middle = (left + right) //2
if (nums[middle] > target):
right=middle-1
elif (nums[middle] < target):
left=middle
else:
return middle
return -14. Open left and right to find
left=0
right=len(nums)-1
while (left <= right):
middle = (left + right) //2
if (nums[middle] > target):
right=middle
elif (nums[middle] < target):
left=middle
else:
return middle
return -13、 ... and . summary
Whatever form of binary search , The overall structure of the code is the same ; The difference lies in the opening and closing of the interval boundary
left=0
right=len(nums)-1
while (left <=/< right): # When the boundary can take the same value <=; Otherwise, it would be <
middle = (left + right) //2
if (nums[middle] > target):
right=middle/middle-1 # Open interval is middle, The closed interval is middle-1
elif (nums[middle] < target):
left=middle/middle+1 # Open interval is middle, The closed interval is middle+1
else:
return middle
return -1Four .Leetcode Title Reference
34. Find the first and last positions of the elements in the sort array
边栏推荐
- Erlang learning 02
- 给你的网站加一个爱发电角标
- 2022, enterprise informatization construction based on Unified Process Platform refers to thubierv0.1
- NiO knowledge points
- Segment tree--
- Adobe substance 3D Designer 2021 software installation package download and installation tutorial
- Arduino + AD9833 waveform generator
- 图像处理:浮点数转定点数
- 常量指针、指针常量
- How does ribbon get the default zoneawareloadbalancer?
猜你喜欢

Arduino + AD9833 waveform generator

Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!

Deployment and analysis of coredns

Sentinel 三种流控模式

图模型2--2022-5-13

MySQL - lock
![[electronic device note 3] capacitance parameters and type selection](/img/d2/1ddb309a8f3cfe5f65c71964052006.png)
[electronic device note 3] capacitance parameters and type selection

数组元素移除问题

Arduino + AD9833 波形发生器

Google cooperates with colleges and universities to develop a general model, proteogan, which can design and generate proteins with new functions
随机推荐
Differential restraint system -- 1 and 2 -- May 27, 2022
String__
[carving master learning programming] Arduino hands-on (59) - RS232 to TTL serial port module
给你的网站加一个爱发电角标
Create a vertical seekbar from scratch
How to solve the problem of robot positioning and navigation in large indoor scenes with low-cost solutions?
A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]
[correcting Hongming] what? I forgot to take the "math required course"!
CMS vulnerability recurrence - ultra vires vulnerability
zoj-Swordfish-2022-5-6
zoj-Swordfish-2022-5-6
N叉树、page_size、数据库严格模式修改、数据库中delect和drop的不同
Basic SQL operations
MySQL——锁
[sword finger offer II 115. reconstruction sequence]
CMS vulnerability recurrence - foreground arbitrary user password modification vulnerability
The concept and representation of a tree
每日三题 7.21
Common Unicode encoding range
How many indexes are associated indexes under MySQL InnoDB?