当前位置:网站首页>Binary search method
Binary search method
2022-06-26 03:48:00 【Just call me ah Jie】
The binary search method is applicable to ordered columns , Judge whether the target value is in the list by taking the number in the middle of the list each time , If the median is greater than the target value , Then the target value is to the left of the middle , On the contrary, it is on the right .
class Solution:
def search(self,list_num:list,tag):
low,high = 0,len(list_num)-1
while low <=high:
mid = (high - low ) // 2 + low
num = list_num[mid]
if num==tag:
return mid
elif num > tag:
high =mid-1 # The intermediate value is greater than the target value , So take the left side of the list , The subscript value on the right is updated to mid-1
elif num <tag:
low = mid +1 # The intermediate value is less than the target value , So take the right side of the list , The subscript value on the left is updated to mid+1
return -1subject :
I'll give you a subscript from 1 The starting array of integers numbers , The array has been pressed Non decreasing order , Please find out from the array that the sum satisfying the addition is equal to the target number target Two numbers of , If there is only one answer .
Example :
Input :numbers = [2,7,11,15], target = 9
Output :[1,2]
explain :2 And 7 The sum is equal to the number of targets 9 . therefore index1 = 1, index2 = 2 . return [1, 2] .
Their thinking :
Suppose a number A, And then this number A, Whether the addition with the following number is equal to the target value , Use the dichotomy method to find the second number
class Solution:
def twoSum(self, numbers: list, target: int) :
n = len(numbers)
for i in range(n):
low,high=i+1,n-1
while low<=high:
mid = (high+low)//2
if numbers[mid] == target-numbers[i]:
return [i+1,mid+1]
elif numbers[mid]>target-numbers[i]:
high = mid-1
else:
low = mid+1
return [-1,-1]
边栏推荐
猜你喜欢

ABP framework Practice Series (III) - domain layer in depth

Can string be changed?

栖霞消防开展在建工地消防安全培训

Non H5 end of uni app, regional setting of status bar on the top of mobile phone

【Flink】Flink 批处理模式Map端数据聚合 NormalizedKeySorter
![[LOJ 6718] nine suns' weakened version (cyclic convolution, arbitrary modulus NTT)](/img/fd/0c299b7cc728f2d6274eea30937726.png)
[LOJ 6718] nine suns' weakened version (cyclic convolution, arbitrary modulus NTT)
![MySQL advanced Chapter 1 (installing MySQL under Linux) [2]](/img/24/0795ebd2a0673d48c5e0f9d18842d1.png)
MySQL advanced Chapter 1 (installing MySQL under Linux) [2]

Evaluation - analytic hierarchy process

Drag and drop

Classic model – RESNET
随机推荐
MySQL addition, deletion, query and modification (Advanced)
MySQL高級篇第一章(linux下安裝MySQL)【下】
169. most elements
2020 summary: industrial software development under Internet thinking
【Appium踩坑】io.appium.uiautomator2.common.exceptions.InvalidArgumentException: ‘capabilities‘ are mand
链路监控 pinpoint
redux-thunk 简单案例,优缺点和思考
Non H5 end of uni app, regional setting of status bar on the top of mobile phone
C # knowledge structure
go time包:秒、毫秒、纳秒时间戳输出
Uni app Baidu cloud realizes OCR ID card recognition
You cannot call Glide. get() in registerComponents(), use the provided Glide instance instead
JS to achieve the effect of text marquee
Camera-CreateCaptureSession
Asynctask multiple simultaneous use methods
Xiaomi TV's web page and jewelry's web page
Prism framework project application - Navigation
【好书集锦】从技术到产品
[paper notes] learning to grasp with primitive shaped object policies
ABP framework Practice Series (II) - Introduction to domain layer