当前位置:网站首页>[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
边栏推荐
- Argo workflows source code analysis
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- 豆瓣平均 9.x,分布式领域的 5 本神书!
- pgpool-II和pgpoolAdmin的使用
- Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
- 长安链学习笔记-证书研究之证书模式
- Stm32f4 --- general timer update interrupt
- 【Node学习笔记】chokidar模块实现文件监听
- Summer Challenge database Xueba notes (Part 2)~
- Overall query process of PostgreSQL
猜你喜欢

Word wrap when flex exceeds width

FLIR blackfly s usb3 industrial camera: how to use counters and timers

Lombok同时使⽤@Data和@Builder 的坑

postgresql之整体查询大致过程

处理streamlit库上传的图片文件

Dall-E Mini的Mega版本模型发布,已开放下载

Lombok makes the pit of ⽤ @data and @builder at the same time

建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!

Detailed explanation of line segment tree (including tested code implementation)

压缩 js 代码就用 terser
随机推荐
Lombok同时使⽤@Data和@Builder 的坑
GEE升级,可以实现一件run tasks
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
本周 火火火火 的开源项目!
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
大咖云集|NextArch基金会云开发Meetup来啦!
豆瓣平均 9.x,分布式领域的 5 本神书!
1个月增长900w+播放!总结B站顶流恰饭的2个新趋势
Use of pgpool II and pgpooladmin
Seconds understand the delay and timing function of wechat applet
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
How can reinforcement learning be used in medical imaging? A review of Emory University's latest "reinforcement learning medical image analysis", which expounds the latest RL medical image analysis co
How to build a 32core raspberry pie cluster from 0 to 1
长安链学习笔记-证书研究之证书模式
Lidar: introduction and usage of ouster OS
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
真实项目,用微信小程序开门编码实现(完结)
Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
argo workflows源码解析
Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)