当前位置:网站首页>[leetcode]Search for a Range
[leetcode]Search for a Range
2022-07-07 02:31:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
A narrative statement of the problem :
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm’s runtime complexity must be in the order ofO(log n).
If the target is not found in the array, return [-1, -1]
.
For example, Given [5, 7, 7, 8, 8, 10]
and target value 8, return [3, 4]
.
The basic idea :
This problem can be solved by binary search .
Suppose there is no target, In the O(lgn) Time is over .
Let's say there's... In the array target, If I found target after , Continue to dichotomy the content before and after Find this location pos .
- Ahead pos Need to meet A[pos] < target && A[pos+1] == target. pos+1 That is the starting position .
- hinder pos Need to meet A[pos] > target && A[pos-1] == target . pos-1 It's the end position .
Code :
vector<int> searchRange(int A[], int n, int target) { //C++
vector<int> result(2);
int low = 0, high = n-1;
int mid, begin = -1, end = -1;
while(low <= high)
{
mid = (low+high)/2;
if(A[mid] > target)
high = mid - 1;
else if(A[mid] < target)
low = mid + 1;
else
{
begin = mid;
end = mid;
//get begin
if(low <= begin -1){
while((low <= begin-1) && !(A[low]<target && A[low+1] == target) )
{
mid = (low + begin-1)/2;
if(A[mid] < target)
low = mid+1;
else begin = mid;
}
if(A[low]<target && A[low+1] == target)
begin = low+1;
}
//get end
if(high >= end+1){
while((high >= end+1) &&!(A[high]>target && A[high-1] == target))
{
mid = (high + end +1)/2;
if(A[mid] > target)
high = mid - 1;
else end = mid;
}
if(A[high]>target && A[high-1] == target)
end = high - 1;
}
break;
}
}
result[0] = begin;
result[1] = end;
return result;
}
Copyright notice : This article is the original article of the blogger , Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116862.html Link to the original text :https://javaforall.cn
边栏推荐
- MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
- leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
- Dall-E Mini的Mega版本模型发布,已开放下载
- 建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- 张平安:加快云上数字创新,共建产业智慧生态
- Introduction to the internal structure of the data directory of PostgreSQL
- [leetcode] day97 remove linked list elements
- Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
猜你喜欢
Processus général de requête pour PostgreSQL
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
Infrared camera: juge infrared mag32 product introduction
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
6-6漏洞利用-SSH安全防御
Data connection mode in low code platform (Part 1)
Web3's need for law
unity 自定义webgl打包模板
随机推荐
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
Recent applet development records
Introduction to FLIR blackfly s industrial camera
Application analysis of face recognition
PCL 常用拟合模型及使用方法
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Processus général de requête pour PostgreSQL
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
[C # notes] use file stream to copy files
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Lumion 11.0软件安装包下载及安装教程
Lumion 11.0 software installation package download and installation tutorial
[leetcode] day97 remove linked list elements
本周 火火火火 的开源项目!
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
ODBC database connection of MFC windows programming [147] (with source code)
[paper reading | deep reading] dngr:deep neural networks for learning graph representations
C#/VB.NET 删除Word文档中的水印
Alibaba cloud middleware open source past
所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身