当前位置:网站首页>[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
边栏推荐
- Infrared camera: juge infrared mag32 product introduction
- STM32 project -- Topic sharing (part)
- C#/VB. Net to delete watermarks in word documents
- C#/VB.NET 删除Word文檔中的水印
- Detailed explanation of line segment tree (including tested code implementation)
- The mega version model of dall-e MINI has been released and is open for download
- 解密函数计算异步任务能力之「任务的状态及生命周期管理」
- unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
- Seconds understand the delay and timing function of wechat applet
- 云原生混部最后一道防线:节点水位线设计
猜你喜欢
3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
Word wrap when flex exceeds width
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Dall-E Mini的Mega版本模型发布,已开放下载
Web3's need for law
Big guys gather | nextarch foundation cloud development meetup is coming!
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
go swagger使用
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
Data connection mode in low code platform (Part 1)
随机推荐
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
postgresql之整体查询大致过程
FLIR blackfly s usb3 industrial camera: how to use counters and timers
Word wrap when flex exceeds width
张平安:加快云上数字创新,共建产业智慧生态
C#/VB. Net to delete watermarks in word documents
Alibaba cloud middleware open source past
[C # notes] reading and writing of the contents of text files
阿里云中间件开源往事
Seconds understand the delay and timing function of wechat applet
Collection recommandée!! Quel plug - in de gestion d'état flutter est le plus fort? Regardez le classement des manons de l'île, s'il vous plaît!
【LeetCode】Day97-移除链表元素
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
Apifox,你的API接口文档卷成这样了吗?
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
6 seconds to understand the book to the Kindle
Go swagger use
企业中台建设新路径——低代码平台
leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
A new path for enterprise mid Platform Construction -- low code platform