当前位置:网站首页>【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};
}
}
边栏推荐
- 【云原生】Nacos中的事件发布与订阅--观察者模式
- Principle and configuration of RSTP protocol
- 跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
- From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code
- 事务的基本特性和隔离级别
- 时钟周期
- Simple page request and parsing cases
- SAP SEGW 事物码里的 Association 建模方式
- Natural language processing series (I) introduction overview
- Taobao short video, why the worse the effect
猜你喜欢

946. Verify stack sequence

Put functions in modules

STM32 and motor development (from architecture diagram to documentation)

《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动

Pycharm installation third party library diagram

Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications

RHCSA9

CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】

Lepton 无损压缩原理及性能分析

It's too convenient. You can complete the code release and approval by nailing it!
随机推荐
Principle and performance analysis of lepton lossless compression
PyCharm安装第三方库图解
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
Introduction to the principle of DNS
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
前缀、中缀、后缀表达式「建议收藏」
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
How do e-commerce sellers refund in batches?
#yyds干货盘点# 解决名企真题:搬圆桌
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
Talk about my drawing skills in my writing career
Navigation property and entityset usage in SAP segw transaction code
LeetCode20.有效的括号
Compile kernel modules separately
##无监控,不运维,以下是监控里常用的脚本监控
Insmod prompt invalid module format
ABAP editor in SAP segw transaction code
Detailed explanation of navigation component of openharmony application development
SAP SEGW 事物码里的 Association 建模方式