当前位置:网站首页>Force buckle 34 finds the first and last positions of elements in a sorted array
Force buckle 34 finds the first and last positions of elements in a sorted array
2022-06-11 18:26:00 【Yingtai night snow】
Power button 34 Find the first and last positions of the elements in the sort array
Title Description
Given an array of integers in ascending order nums, And a target value target. Find the start 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].
The design and implementation time complexity is O(log n) The algorithm of
I/o sample
Input :nums = [5,7,7,8,8,10], target = 8
Output :[3,4]
Input :nums = [5,7,7,8,8,10], target = 6
Output :[-1,-1]
Input :nums = [], target = 0
Output :[-1,-1]
Algorithm one , Use one bisection to find the target location, and then iteratively find the left endpoint and the right endpoint
vector<int>searchRange(vector<int>&nums,int target)
{
vector<int>res;
int length=nums.size();
int left=0,right=length-1;
int numIndex=-1;
if(length==1)
{
(nums[0]==target)?res={
0,0}:res={
-1,-1};
return res;
}
while(left<=right)
{
int mid=(left+right)/2;
if(nums[mid]==target)
{
numIndex=mid;
break;
}
else if(nums[mid]<target)
{
left=mid+1;
}
else{
right=mid-1;
}
}
if(numIndex==-1)
{
res={
-1,-1};
}
else
{
int leftIndex=numIndex;
int rightIndex=numIndex;
while(leftIndex>=0&&nums[leftIndex]==target)
{
leftIndex--;
}
while(rightIndex<length&&nums[rightIndex]==target)
{
rightIndex++;
}
res.push_back(leftIndex+1);
res.push_back(rightIndex-1);
}
return res;
}
Algorithm 2 , Use two bisections , Find the left endpoint and the right endpoint
// Use two bisections to find the corresponding upper and lower bounds
vector<int> searchRange2(vector<int>&nums,int target)
{
if(nums.empty())
{
return {
-1,-1};
}
// Create values for binary lookups
int left=0,right=nums.size()-1;
// Find where the element first appears
while(left<right)
{
int mid=(left+right)/2;
if(nums[mid]>=target)
{
right=mid;
}
else if(nums[mid]<target)
{
left=mid+1;
}
}
if(nums[left]!=target)
{
return {
-1,-1};
}
int leftIndex=left;
// Find where the element last appeared
left=0;
right=nums.size()-1;
while(left<right)
{
// Avoid the dead cycle
int mid=(left+right+1)/2;
if(nums[mid]<=target)
{
left=mid;
}
else if(nums[mid]>target)
{
right=mid-1;
}
}
// The overload has been determined before, so there is no need to judge whether the search fails again
int rightIndex=right;
return {
leftIndex,rightIndex};
}
边栏推荐
- 牛客刷题——part8
- 牛客刷题——二叉搜索树与双向链表
- .net core redis hyperloglog类型
- Explain AI accelerators in detail: GPU, DPU, IPU, TPU... There are infinite possibilities for AI acceleration schemes
- Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules
- [C语言]对一个数组的元素排序后平移元素
- 平衡搜索二叉树——AVL树
- viso的常见操作
- Feign 共享登录信息进行请求
- 牛客刷题——part7
猜你喜欢

ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"

264 Concepts

VIM common commands

Ubuntu installs PSQL and runs related commands

Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules

Oracle高级数据库复习

SISO Decoder for Repetition(补充章节4)

JS实现全屏展示的具体方法

软件需求工程复习

牛客刷题——二叉搜索树与双向链表
随机推荐
Explain AI accelerators in detail: GPU, DPU, IPU, TPU... There are infinite possibilities for AI acceleration schemes
MMA-Self-defining function
排序的循环链表
IEDA 底层菜单菜单简介
SQL error injection 1
网络和并发编程常见面试题
DataNode的启动流程
力扣刷题——二叉树的层序遍历Ⅱ
. Net core redis hyperloglog type
Class question: how to ensure that line table storage can be inserted at any time?
力扣刷题——根据二叉树创建字符串
New work of "the father of LSTM": a new method towards self correcting neural network
Use egg Js+mongodb simple implementation of curdapi
Function and principle of key in V-for
力扣31 下一个排列
力扣33题,搜索旋转排序数组
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
[golang] leetcode - 349 Intersection of two arrays (hash table)
v-for循环遍历
牛客刷题——Fibonacci数列