当前位置:网站首页>【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};
}
}
边栏推荐
- 石臻臻的2021总结和2022展望 | 文末彩蛋
- RHCAS6
- Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
- Hiengine: comparable to the local cloud native memory database engine
- CloudCompare——点云切片
- Reflection and imagination on the notation like tool
- APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
- Changing JS code has no effect
- Flutter 绘制波浪移动动画效果,曲线和折线图
- 初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
猜你喜欢

石臻臻的2021总结和2022展望 | 文末彩蛋

Natural language processing series (I) introduction overview

Leetcode20. Valid parentheses

Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function

What is the difference between Bi software in the domestic market

百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程

Didi open source Delta: AI developers can easily train natural language models

数据湖(七):Iceberg概念及回顾什么是数据湖

Taobao product details API | get baby SKU, main map, evaluation and other API interfaces

A specific example of ABAP type and EDM type mapping in SAP segw transaction code
随机推荐
将函数放在模块中
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
数据泄露怎么办?'华生·K'7招消灭安全威胁
Halcon template matching actual code (I)
SAP UI5 FlexibleColumnLayout 控件介绍
【Nacos云原生】阅读源码第一步,本地启动Nacos
RHCSA5
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
SAP SEGW 事物码里的 Association 建模方式
MySQL splits strings for conditional queries
Word document injection (tracking word documents) incomplete
使用Dom4j解析XML
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
Didi open source Delta: AI developers can easily train natural language models
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
Yyds dry inventory JS intercept file suffix
量价虽降,商业银行结构性存款为何受上市公司所偏爱?
碎片化知识管理工具Memos
Laravel document reading notes -mews/captcha use (verification code function)