当前位置:网站首页>【Hot100】34. Find the first and last positions of elements in a sorted array
【Hot100】34. Find the first and last positions of elements in a sorted array
2022-07-05 13:14:00 【Wang Liuliu's it daily】
34. Find the first and last positions of the elements in the sort array
Medium question
Give you an answer The decreasing An array of integers arranged in sequence nums, And a target value target.
Please find out the start position and end position of the given target value in the array .
If the target value does not exist in the array target, return [-1, -1].
You must design and implement a time complexity of O(log n) The algorithm to solve this problem .
Answer key :
Within a range , Find a number , Ask to find the start and end positions of this element , The numbers in this range are monotonically increasing , That is, it has monotonic property , So you can use dichotomy to do .
Two bisections , The first dichotomy finds the first t>=target The location of , The second dichotomy looks for the last t<=target The location of .
If the search is successful, two position subscripts will be returned , Otherwise return to [-1,-1].
- Template I : When we will interval [l, r] Divide into [l,mid] and [mid + 1, r] when , The update operation is r = mid perhaps l = mid + 1, Calculation mid There is no need to add 1, namely mid=(l+r)/2.
- Template II : When we will interval [l, r] Divide into [l,mid−1] and [mid,r] when , The update operation is r=mid−1 perhaps l=mid, In order to prevent the dead cycle , Calculation mid Need to add 1, namely mid=(l+r+1)/2.
Be careful :
When the left boundary is to be updated to l = mid when , Order mid =(l + r + 1)/2, Equivalent to rounding up , It won't be because r Take a special value r = l + 1 And fall into a dead circle .
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; // Dichotomous range
while( l < r) // Find the starting position of the element
{
int mid = (l + r )/2;
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}
if( nums[r] != target) {
return new int[]{
-1,-1}; // To find the failure
}
int L = r;
l = 0;
r = nums.length - 1; // Dichotomous range
while( l < r) // Find the end position of the element
{
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; // Dichotomous range Left and right closed
// Find the starting position of the element
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}; // To find the failure
}
int L = r;
l = 0; r = nums.length - 1; // Dichotomous range
// Find the end position of the element
while( l < r)
{
int mid = (l + r + 1)/2; // Be careful
if(nums[mid] <= target ){
l = mid;
} else {
r = mid - 1;
}
}
return new int[]{
L,r};
}
}
边栏推荐
- leetcode:221. Maximum square [essence of DP state transition]
- 将函数放在模块中
- There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)
- DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
- 解决uni-app配置页面、tabBar无效问题
- 函数的默认参数&函数参数的多种方法
- js判断数组中是否存在某个元素(四种方法)
- [notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
- 阿里云SLB负载均衡产品基本概念与购买流程
猜你喜欢

解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107

I'm doing open source in Didi

RHCSA9

将函数放在模块中

百度杯”CTF比赛 2017 二月场,Web:爆破-2

SAP UI5 DynamicPage 控件介紹

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

STM32 and motor development (from architecture diagram to documentation)
![[cloud native] use of Nacos taskmanager task management](/img/ad/24bdd4572ef9990238913cb7cd16f8.png)
[cloud native] use of Nacos taskmanager task management

MySQL 巨坑:update 更新慎用影响行数做判断!!!
随机推荐
C# 对象存储
Rocky basics 1
leetcode:221. Maximum square [essence of DP state transition]
前缀、中缀、后缀表达式「建议收藏」
I'm doing open source in Didi
Small case of function transfer parameters
Get to know linkerd project for the first time
SAP ui5 objectpagelayout control usage sharing
There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
解决uni-app配置页面、tabBar无效问题
MySQL 巨坑:update 更新慎用影响行数做判断!!!
CAN和CAN FD
uni-app开发语音识别app,讲究的就是简单快速。
RHCSA9
【Hot100】33. 搜索旋转排序数组
Flutter InkWell & Ink组件
解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
百度杯”CTF比赛 2017 二月场,Web:爆破-2
go 字符串操作