当前位置:网站首页>【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};
}
}
边栏推荐
- [深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
- Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
- MSTP and eth trunk
- A specific example of ABAP type and EDM type mapping in SAP segw transaction code
- 将函数放在模块中
- #yyds干货盘点# 解决名企真题:搬圆桌
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
- Realize the addition of all numbers between 1 and number
- Rocky基础知识1
- 手把手带你入门Apache伪静态的配置
猜你喜欢
百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程
Simple page request and parsing cases
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
946. 验证栈序列
Introduction to sap ui5 dynamicpage control
Talk about my drawing skills in my writing career
百度杯”CTF比赛 2017 二月场,Web:爆破-2
函数传递参数小案例
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
随机推荐
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
How to protect user privacy without password authentication?
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
946. Verify stack sequence
国际自动机工程师学会(SAE International)战略投资几何伙伴
关于 Notion-Like 工具的反思和畅想
mysql econnreset_ Nodejs socket error handling error: read econnreset
一文详解ASCII码,Unicode与utf-8
A small talk caused by the increase of sweeping
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
uni-app开发语音识别app,讲究的就是简单快速。
MySQL splits strings for conditional queries
Rocky基础命令3
Leetcode20. Valid parentheses
Flutter 绘制波浪移动动画效果,曲线和折线图
程序员成长第八篇:做好测试工作
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
阿里云SLB负载均衡产品基本概念与购买流程
Lb10s-asemi rectifier bridge lb10s
RHCSA9