[Leetcode] search for a range 寻找范围
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 of O(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].
题意:找出目标值在给定序列中起始和终止下标值。
思路:这题关键的是对时间复杂度限制为O(logn),所以,应该是二分查找算法的变形。刚开始,我还是想用一般的二分查找,找到等于目标值的下标了,然后向两边推进找到左右边界(此时,注意的下标)。但这种方法当重复的数字比较多的时,时间复杂度远不止O(logn),见代码一。
方法二,分别用二分查找找到这个序列的左右边界,即可。这个方法中值得注意的是,选取边界条件时,if语句中的条件判断。见代码二:
代码一:
class Solution {
public:
vector<int> searchRange(int A[], int n, int target)
{
int lo=,hi=n;
while(lo<hi)
{
int mid=lo+(hi-lo)/;
if(A[mid]==target)
break;
else if(target<A[mid])
hi=mid;
else
lo=mid+;
}
if(A[mid] !=target)
return {-,-};
lo=mid,hi=mid;
while(lo>=&&A[lo]==target)
lo--;
while(hi<n&&A[hi]==target)
hi++;
return {lo,hi};
}
};
参考了Grandyang的博客,代码二:
class Solution {
public:
vector<int> searchRange(int A[], int n, int target)
{
vector<int> res(,-);
int lo=,hi=n;
//找左边界
while(lo<hi)
{
int mid=lo+(hi-lo)/;
if(A[mid]<target)
lo=mid+;
else
hi=mid;
}
if(A[hi] !=target)
return res;
res[]=hi;
//右边界
hi=n;
while(lo<hi)
{
int mid=lo+(hi-lo)/;
if(target<A[mid])
hi=mid;
else
lo=mid+;
}
res[]=lo-;
return res;
}
};
[Leetcode] search for a range 寻找范围的更多相关文章
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- 「日常训练」COMMON 约数研究(HYSBZ-1968)
题意与分析 感谢https://www.cnblogs.com/Leohh/p/7512960.html的题解.这题话说原来不在我的训练范围,正好有个同学问我,我就拿来做做.数学果然不是我擅长的啊,这 ...
- MySQL☞substr函数
substr函数:截取字符串 格式如下: select substr(参数1,参数2,参数3) from 表名 参数1:列名/字符串 参数2:起始位置,如果为正数,就表示从正数的位置往下截取字符 ...
- Selenium基础之--01(将浏览器最大化,设置浏览器固定宽、高,操控浏览器前进、后退)
1,将浏览器最大化 我们知道调用启动的浏览器不是全屏的,这样不会影响脚本的执行,但是有时候会影响我们"观看"脚本的执行. coding=utf-8 from selenium im ...
- BehaviorDesigner学习
行为树: 行为树设计师插件是一个专门为unity设计的AI插件. 学习用!!!插件地址:链接:http://pan.baidu.com/s/1dF2okPN 密码:b43m 通过继承Behavior中 ...
- 下拉网页div自动浮在顶部
<!DOCTYPE html> <html> <head> <title></title> <style type="tex ...
- docker学习2
今天继续学习docker! 搜索镜像 docker search centos 下载镜像 docker pull name(镜像名字) 查看镜像docker images 字段含义分析: TAG:仓库 ...
- Exact Inference in Graphical Models
独立(Independence) 统计独立(Statistical Independence) 两个随机变量X,Y统计独立的条件是当且仅当其联合概率分布等于边际概率分布之积: \[ X \perp Y ...
- 团队作业week9 情景测试
一.使用人群:学生.计算机工作者.对计算机感兴趣的人 1.学生:学生是学霸系统的主要用户.学生一般会通过网络寻找与自己的课程,作业有关的信息.首先,可以通过我们的搜索功能在我们的数据库中寻找我们从网络 ...
- HBase 参考文档翻译之 Getting Started
本篇是对HBase官方参考文档的大体翻译,介于本人英文水平实在有限,难免有纰漏之处.本篇不只是对官方文档的翻译,还加入了一些本人对HBase的理解.在翻译过程中,一些没有营养的废话,我就忽略了没有翻译 ...
- 第一周—Fortran语言学习
使用教材:Fortran95程序设计[彭国伦] 第二章 编译器的使用 编译结果的好坏 1.翻译正确 2.执行文件的运行效率 3.翻译出来的执行码的长短 4.编译过程花费的时间 5.编译器提供Debug ...