当前位置:网站首页>【Hot100】34. 在排序数组中查找元素的第一个和最后一个位置
【Hot100】34. 在排序数组中查找元素的第一个和最后一个位置
2022-07-05 12:56:00 【王六六的IT日常】
34. 在排序数组中查找元素的第一个和最后一个位置
中等题
给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。
请你找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
题解:
在一个范围内,查找一个数字,要求找到这个元素的开始位置和结束位置,这个范围内的数字都是单调递增的,即具有单调性质,因此可以使用二分来做。
两次二分,第一次二分查找第一个t>=target的位置,第二次二分查找最后一个t<=target的位置。
查找成功则返回两个位置下标,否则返回[-1,-1]。
- 模板一:当我们将区间[l, r]划分成[l,mid]和[mid + 1, r]时,其更新操作是r = mid或者l = mid + 1,计算mid时不需要加1,即mid=(l+r)/2。
- 模板二:当我们将区间[l, r]划分成[l,mid−1]和[mid,r]时,其更新操作是r=mid−1或者l=mid,此时为了防止死循环,计算mid时需要加1,即mid=(l+r+1)/2。
注意:
当左边界要更新为l = mid时,就令 mid =(l + r + 1)/2,相当于上取整,此时就不会因为r取特殊值 r = l + 1而陷入死循环了。
class Solution {
public int[] searchRange(int[] nums, int target) {
if(nums.length == 0) {
return new int[]{
-1,-1};
}
int l = 0, r = nums.length - 1; //二分范围
while( l < r) //查找元素的开始位置
{
int mid = (l + r )/2;
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}
if( nums[r] != target) {
return new int[]{
-1,-1}; //查找失败
}
int L = r;
l = 0;
r = nums.length - 1; //二分范围
while( l < r) //查找元素的结束位置
{
int mid = (l + r + 1)/2;
if(nums[mid] <= target ) l = mid;
else r = mid - 1;
}
return new int[]{
L,r};
}
}
class Solution {
public int[] searchRange(int[] nums, int target) {
if(nums.length == 0){
return new int[]{
-1,-1};
}
int l = 0, r = nums.length - 1; //二分范围 左闭右闭
//查找元素的开始位置
while( l < r){
int mid = (l + r )/2;
if(nums[mid] >= target){
r = mid;
} else{
l = mid + 1;
}
}
if( nums[r] != target){
return new int[]{
-1,-1}; //查找失败
}
int L = r;
l = 0; r = nums.length - 1; //二分范围
//查找元素的结束位置
while( l < r)
{
int mid = (l + r + 1)/2; //注意
if(nums[mid] <= target ){
l = mid;
} else {
r = mid - 1;
}
}
return new int[]{
L,r};
}
}
边栏推荐
- Insmod prompt invalid module format
- ASEMI整流桥HD06参数,HD06图片,HD06应用
- 蜀天梦图×微言科技丨达梦图数据库朋友圈+1
- 自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
- Halcon template matching actual code (I)
- ##无监控,不运维,以下是监控里常用的脚本监控
- Yyds dry inventory JS intercept file suffix
- 时钟周期
- Concurrent performance test of SAP Spartacus with JMeter
- 你的下一台电脑何必是电脑,探索不一样的远程操作
猜你喜欢

Detailed explanation of navigation component of openharmony application development

Natural language processing series (I) introduction overview

LB10S-ASEMI整流桥LB10S

From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code

RHCAS6

量价虽降,商业银行结构性存款为何受上市公司所偏爱?

How can non-technical departments participate in Devops?

Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications

Cf:a. the third three number problem

阿里云SLB负载均衡产品基本概念与购买流程
随机推荐
Association modeling method in SAP segw transaction code
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
解决uni-app配置页面、tabBar无效问题
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
Lb10s-asemi rectifier bridge lb10s
SAP SEGW 事物码里的 Association 建模方式
RHCAS6
Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
Didi open source Delta: AI developers can easily train natural language models
解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
Taobao short video, why the worse the effect
leetcode:221. 最大正方形【dp状态转移的精髓】
Put functions in modules
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
HiEngine:可媲美本地的云原生内存数据库引擎
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
RHCSA9
RHCSA1
uni-app开发语音识别app,讲究的就是简单快速。
Reflection and imagination on the notation like tool