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 寻找范围的更多相关文章

  1. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

  2. leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  3. [LeetCode] Search for a Range 搜索一个范围

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. [LeetCode] Search for a Range(二分法)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  5. leetcode Search for a Range python

    class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...

  6. [LeetCode] Search for a Range 二分搜索

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  7. Leetcode Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  8. leetcode:Search for a Range(数组,二分查找)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. leetcode -- Search for a Range (TODO)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. 「赛后补题」Meeting(HDU-5521)

    题意 A,B两个人分别在1和n区.每个区有若干点(区之间的点可以重复,各个区内点间的距离一致),给出区之间有联系的图以及到达所需时间.求两个人见面最短时间以及在哪个区碰面(可有多个) 分析 隐式图搜索 ...

  2. 「日常训练」 Counting Cliques(HDU-5952)

    题意与分析 题源:2016ACM/ICPC沈阳现场赛. 这题让我知道了什么是团,不过最恶心的还是这题的数据了,卡了无数次- - 解决方法是维护一个G数组,不能去遍历邻接矩阵.至少我改了这么一个地方就过 ...

  3. <cfenv>(fenv.h) _c++11

    头文件 <cfenv>(fenv.h) c++11 浮点环境 这个头文件声明了一系列的函数和宏去访问浮点环境,以及特殊的类型. 浮点环境维护一系列的状态标志(status flags)和具 ...

  4. Pyhton网络爬虫实例_豆瓣电影排行榜_BeautifulSoup4方法爬取

    -----------------------------------------------------------学无止境------------------------------------- ...

  5. 深入理解java虚拟机学习笔记(一)

    第二章 Java内存区域与内存溢出异常 运行时数据区域 程序计数器(Program Counter Register) 程序计数器:当前线程所执行的字节码行号指示器.各条线程之间计数器互不影响,独立存 ...

  6. 给eclipse安装color-theme插件

    给eclipse安装color-theme插件 2016年03月22日 19:16:01 ming_love 阅读数:5193 标签: Eclipse Color Theme 更多 个人分类: jav ...

  7. matconv-GPU 编译问题

    如出现以下错误: 1 error detected in the compilation of "C:/Users/Justin/AppData/Local/Temp/tmpxft_0000 ...

  8. Python数据分析实战-Boston Public Schools GEO数据分析-Part1

    项目目标: Boston Public Schools Geo数据是来自于Boston地区的公共学校的数据,具体描述了学校的坐标,名字,类型等.基于此数据,我们可以学习一些基本的Python数据分析的 ...

  9. C#程序 权限不够的解决方案

    有时候需要操作硬件,或者启动windows服务程序时,系统会提示很多奇怪的问题,归根结底就是程序当前所拥有的权限不够,需要提升,以前我们时手写一个manifest,多不容易啊, 现在有正常的方法了 1 ...

  10. Linux 添加虚拟网卡

    使用的Linux版本是Centos 7: [root@vnode33 bin]# cat /etc/redhat-release CentOS Linux release (Core) 使用ifcon ...