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. DEDEcms调用当前栏目顶级栏目url地址

    include/common.func.php 找到这个文件 在文件最下方加入以下代码: //获取顶级栏目url function GetTopTypeurl($id) { global $dsql; ...

  2. 第四模块:网络编程进阶&数据库开发 第1章·网络编程进阶

    01-进程与程序的概念 02-操作系统介绍 03-操作系统发展历史-第一代计算机 04-操作系统发展历史-批处理系统 05-操作系统发展历史-多道技术 06-操作系统发展历史-分时操作系统 07-总结 ...

  3. Python常用函数--return 语句

    在Python教程中return 语句是函数中常用的一个语句.return 语句用于从函数中返回,也就是中断函数.我们也可以选择在中断函数时从函数中返回一个值.案例(保存为 function_retu ...

  4. 了解Python控制流语句——while 语句

    while 语句 Python 中 while 语句能够让你在条件为真的前提下重复执行某块语句. while 语句是 循环(Looping) 语句的一种.while 语句同样可以拥有 else 子句作 ...

  5. 关于axios跨域带cookie

    axios  设置 withCredentials :true $u = $_SERVER['HTTP_REFERER'];$u = preg_replace('#/$#', '', $u);head ...

  6. python 终极篇 --- django 路由系统

                          URL配置                        基本格式: from django.conf.urls import url urlpattern ...

  7. SSH:远程登陆

    SSH用于计算机之间的加密登录的前提是公钥为真,所以存在中间人攻击中间人攻击:与https协议不同,SSH协议的公钥是没有CA公证的,当对公钥的请求被中间截获时,中间人可以发出伪造公钥干坏事而不被识破 ...

  8. CodeForces 838B Diverging Directions 兼【20180808模拟测试】t3

    描述 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点到达. 2. 接下来的 N-1 ...

  9. centos 6.5 启动时卡在进度条位置无法进入系统解决办法。

    今天公司服务器因突然断电导致phddns 花生壳 启动失败,一直卡在启动进度条页面. 解决办法 1.按F5查看卡在什么位置, 2.查看解决方法:程序卡住的情况下,直接备份资料后,卸载程序重启就可以了. ...

  10. day-17 L1和L2正则化的tensorflow示例

    机器学习中几乎都可以看到损失函数后面会添加一个额外项,常用的额外项一般有两种,一般英文称作ℓ1-norm和ℓ2-norm,中文称作L1正则化和L2正则化,或者L1范数和L2范数.L2范数也被称为权重衰 ...