当前位置:网站首页>【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};
}
}
边栏推荐
- 《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
- How can non-technical departments participate in Devops?
- CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
- uni-app开发语音识别app,讲究的就是简单快速。
- MySQL splits strings for conditional queries
- 函数传递参数小案例
- LeetCode20.有效的括号
- Talk about seven ways to realize asynchronous programming
- Introduction to the principle of DNS
- Introduction to sap ui5 flexiblecolumnlayout control
猜你喜欢
随机推荐
How to protect user privacy without password authentication?
155. 最小栈
Go pointer
“百度杯”CTF比赛 九月场,Web:SQL
go map
LB10S-ASEMI整流桥LB10S
Apicloud studio3 API management and debugging tutorial
uni-app开发语音识别app,讲究的就是简单快速。
Overflow toolbar control in SAP ui5 view
Hiengine: comparable to the local cloud native memory database engine
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Pycharm installation third party library diagram
Association modeling method in SAP segw transaction code
Shandong University Summer Training - 20220620
AVC1与H264的区别
Reverse Polish notation
#yyds干货盘点# 解决名企真题:搬圆桌
一文详解ASCII码,Unicode与utf-8







![[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
