当前位置:网站首页>[leetcode]Search for a Range
[leetcode]Search for a Range
2022-07-06 18:48:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
问题叙述性说明:
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].
基本思路:
此题能够用二分查找法解决。
假设数组中没有target,能够在O(lgn)时间完毕。
假设数组中有target,当发现target后,对前后的内容继续用二分法 查找这种位置pos 。
- 前面的pos须要满足 A[pos] < target && A[pos+1] == target. pos+1即是開始位置。
- 后面的pos须要满足 A[pos] > target && A[pos-1] == target . pos-1是结束位置。
代码:
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;
}版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116862.html原文链接:https://javaforall.cn
边栏推荐
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- 机器人队伍学习方法,实现8.8倍的人力回报
- 压缩 js 代码就用 terser
- 处理streamlit库上传的图片文件
- How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
- The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
- [paper reading | deep reading] dngr:deep neural networks for learning graph representations
- 3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
- 【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例
- argo workflows源码解析
猜你喜欢

老板被隔离了

【论文阅读|深读】RolNE: Improving the Quality of Network Embedding with Structural Role Proximity

Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!

Introduction to FLIR blackfly s industrial camera

AWS学习笔记(一)
![leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]](/img/62/d4d5428f69fc221063a4f607750995.png)
leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]

企业中台建设新路径——低代码平台

1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动

解密函数计算异步任务能力之「任务的状态及生命周期管理」
![[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks](/img/06/17acf9958228cce5d80ada3275ad24.png)
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
随机推荐
【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例
Detailed explanation of line segment tree (including tested code implementation)
UC伯克利助理教授Jacob Steinhardt预测AI基准性能:AI在数学等领域的进展比预想要快,但鲁棒性基准性能进展较慢
Web开发小妙招:巧用ThreadLocal规避层层传值
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Time synchronization of livox lidar hardware -- PPS method
Compress JS code with terser
#夏日挑战赛#数据库学霸笔记(下)~
What to do when encountering slow SQL? (next)
真实项目,用微信小程序开门编码实现(完结)
Linear list --- circular linked list
C#/VB. Net to delete watermarks in word documents
C语言练习题_1
1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
Processing image files uploaded by streamlit Library
ODBC database connection of MFC windows programming [147] (with source code)
3D laser slam: time synchronization of livox lidar hardware
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
Introduction to the internal structure of the data directory of PostgreSQL