当前位置:网站首页>[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
边栏推荐
- SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!
- Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
- Stm32f4 --- PWM output
- Seconds understand the delay and timing function of wechat applet
- Draco - gltf model compression tool
- Use of pgpool II and pgpooladmin
- 【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
- 3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
- unity 自定义webgl打包模板
- Web3对法律的需求
猜你喜欢

Big guys gather | nextarch foundation cloud development meetup is coming!

Compress JS code with terser

如何从0到1构建32Core树莓派集群
![[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files](/img/20/f7fc2204ca165dcea4af25cb054e9b.png)
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files

大咖云集|NextArch基金会云开发Meetup来啦!

Go swagger use

C#/VB. Net to delete watermarks in word documents

C#/VB.NET 删除Word文档中的水印

强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向

leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
随机推荐
Dall-E Mini的Mega版本模型发布,已开放下载
Sensor: DS1302 clock chip and driver code
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
postgresql之integerset
C语言练习题_1
所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身
Web3's need for law
老板被隔离了
[C # notes] reading and writing of the contents of text files
豆瓣平均 9.x,分布式领域的 5 本神书!
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Infrared camera: juge infrared mag32 product introduction
Introduction to the internal structure of the data directory of PostgreSQL
处理streamlit库上传的图片文件
【森城市】GIS数据漫谈(二)
进程管理基础
C # / vb. Net supprime le filigrane d'un document word
安全交付工程师
#yyds干货盘点# 解决名企真题:最大差值
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem