当前位置:网站首页>Binary search common questions
Binary search common questions
2022-07-24 07:41:00 【yn20000227】
1. Find the first one greater than K The elements of
nums = [5,7,7,8,8,9,10,10]
target =8
left,right = 0,len(nums)-1
while(left<=right):
mid = left+(right-left)//2
if nums[mid]<=target:
left = mid+1
elif nums[mid]>target:
right = mid-1
left2. Find the last one ≤k The elements of
nums = [5,7,7,8,8,9,10,10]
target =8
left,right = 0,len(nums)-1
while(left<=right):
mid = left+(right-left)//2
if nums[mid]<=target:
left = mid+1
elif nums[mid]>target:
right = mid-1
right3. Find the first ≥k The elements of
nums = [5,7,7,8,8,9,10,10]
target =8
left,right = 0,len(nums)-1
while(left<=right):
mid = left+(right-left)//2
if nums[mid]<target:
left = mid+1
elif nums[mid]>=target:
right = mid-1
left4. Find the last less than k The elements of
nums = [5,7,7,8,8,9,10,10]
target =8
left,right = 0,len(nums)-1
while(left<=right):
mid = left+(right-left)//2
if nums[mid]<target:
left = mid+1
elif nums[mid]>=target:
right = mid-1
rightOne 、 Find... In an ordered array Numbers K Number of occurrences
The first solution :
1. Find the last one Less than k Coordinates of the number of l ;
2. Find the first Greater than k Coordinates of the number of r ;
3. be Numbers k The number of :r-l-1
The second solution :
1. find The first one is greater than k The elements of r
2. Find the first one greater than k-1 The elements of l
3. be k The number of r-l
class Solution:
def search(self, nums: List[int], target: int) -> int:
def searchleft(target):
left,right = 0,len(nums)-1
while(left<=right):
mid = left+(right-left)//2
if nums[mid]<=target:
left = mid+1
elif nums[mid]>target:
right = mid-1
return right
l = searchleft(target-1)
r = searchleft(target)
return r-l边栏推荐
- Arduino's super power-saving sleep mode has worked with one 18650 battery for 17 years
- Appium use
- FlinkSQL-UDF自定义数据源
- C language to achieve mine sweeping game
- 【信息系统项目管理师】第七章 复盘成本管理知识架构
- [sklearn] RF cross validation out of bag data parameter learning curve grid search
- oracle中有A,B连个表,这两个表需要第三个表C关联,那怎么将A表中的字段MJ1更新为B表中MJ2的值
- 多种优化方法打印100~200之间的素数
- Mitre att & CK ultra detailed learning notes-01 (background, terms, cases)
- Install librosa using Tsinghua image
猜你喜欢
![[cloud native] MySQL index analysis and query optimization](/img/ca/79783721637641cb8225bc26a8c4a9.png)
[cloud native] MySQL index analysis and query optimization

Who can stand it when the project goes online

无法自动装配,未找到“RedisTemplate类型的Bean

XSS vulnerability learning

requests-爬虫实现一个简易网页采集器

Requests crawler multi page crawling to KFC restaurant location

Mutual implementation of stack and queue (c)

C语言文件操作

requests-爬取页面源码数据

Influxdb unauthorized access & CouchDB permission bypass
随机推荐
FlinkSQL-UDF自定义数据源
Selenium basic knowledge automatic search
Train-clean-100 dataset
2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <
numpy.concatenate
Source code analysis of Nacos configuration center
【信息系统项目管理师】第七章 复盘成本管理知识架构
Induction, generalization, deduction
InjectFix原理学习(实现修复加法的热更)
R语言手写数字识别
requests-爬取页面源码数据
Game three piece chess
Unable to auto assemble, bean of type "redistemplate" not found
【Pytorch】Dataset_ DataLoader
Install librosa using Tsinghua image
Selenium basic knowledge paging processing
[sklearn] data preprocessing
24.全局事件总线
Jackson parsing JSON detailed tutorial
MySQL statement