当前位置:网站首页>[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版发布,免费体验降本增效高级功能!
- 【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
- 3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
- Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
- 机器人队伍学习方法,实现8.8倍的人力回报
- Freeswitch dials extension number source code tracking
- 【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
- postgresql之integerset
- 阿里云中间件开源往事
猜你喜欢
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
Pioneer of Web3: virtual human
The last line of defense of cloud primary mixing department: node waterline design
处理streamlit库上传的图片文件
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
Infrared camera: juge infrared mag32 product introduction
Compress JS code with terser
Web3对法律的需求
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
随机推荐
A new path for enterprise mid Platform Construction -- low code platform
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
fiddler的使用
Big guys gather | nextarch foundation cloud development meetup is coming!
所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身
Yyds dry goods inventory # solve the real problem of famous enterprises: maximum difference
机器人队伍学习方法,实现8.8倍的人力回报
新一代云原生消息队列(一)
New generation cloud native message queue (I)
Real project, realized by wechat applet opening code (end)
4 -- Xintang nuc980 mount initramfs NFS file system
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
Metaforce force meta universe development and construction - fossage 2.0 system development
pgpool-II和pgpoolAdmin的使用
C#/VB. Net to delete watermarks in word documents
[unity notes] screen coordinates to ugui coordinates
【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例
Pgadmin4 of PostgreSQL graphical interface tool
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient