当前位置:网站首页>【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};
}
}
边栏推荐
- What is the difference between Bi software in the domestic market
- 155. 最小栈
- Introduction to sap ui5 flexiblecolumnlayout control
- 爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
- 碎片化知识管理工具Memos
- 石臻臻的2021总结和2022展望 | 文末彩蛋
- 前缀、中缀、后缀表达式「建议收藏」
- Taobao short video, why the worse the effect
- SAP UI5 FlexibleColumnLayout 控件介绍
- SAP ui5 objectpagelayout control usage sharing
猜你喜欢
It's too convenient. You can complete the code release and approval by nailing it!
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
简单上手的页面请求和解析案例
About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
国内市场上的BI软件,到底有啥区别
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
Actual combat simulation │ JWT login authentication
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
How can non-technical departments participate in Devops?
随机推荐
#从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题
SAP SEGW 事物码里的 Association 建模方式
Word document injection (tracking word documents) incomplete
Principle and configuration of RSTP protocol
单独编译内核模块
How to realize batch sending when fishing
Association modeling method in SAP segw transaction code
SAP UI5 视图里的 OverflowToolbar 控件
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
Changing JS code has no effect
Natural language processing from Xiaobai to proficient (4): using machine learning to classify Chinese email content
A deep long article on the simplification and acceleration of join operation
Apicloud studio3 API management and debugging tutorial
SAP UI5 DynamicPage 控件介紹
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
Introduction aux contrôles de la page dynamique SAP ui5
从外卖点单浅谈伪需求
LeetCode20.有效的括号
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
事务的基本特性和隔离级别