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. 一个只有十行的精简MVVM框架(下篇)

    本文来自网易云社区. 让我们来加点互动 前面学生信息的身高的单位都是默认m,如果新增一个需求,要求学生的身高的单位可以在m和cm之间切换呢? 首先需要一个变量来保存度量单位,因此这里必须用一个新的Mo ...

  2. Oracle DELETE和TRUNCATE 的区别

    语法delete from aa truncate table aa 区别 1.delete from后面可以写条件,truncate不可以. 2.delete from记录是一条条删的,所删除的每行 ...

  3. 分布式部署Apache-Jmeter粗略流程

    注意事项 Windows版和Mac版Jmeter可互相通信 确认被部署的机器安装有JDK并已配置好环境变量 Controller安装 1. 安装Jmeter,监视插件JMeterPlugins-Sta ...

  4. TPO-13 C2 How to use language lab

    TPO-13 C2 How to use language lab 第 1 段 1.Listen to a conversation between a student and the languag ...

  5. js屏蔽/过滤 特殊字符,输入就删除掉,实时删除

    1.替换方法: <input type="text" class="domain" onkeyup="this.value=this.value ...

  6. CSP201403-2:窗口

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  7. CSP201709-1:打酱油

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  8. ES6 之 let / const

    本博文配合 阮一峰 <ES6 标准入门(第3版)>一书进行简要概述 ES6 中的 let 与 const. 历史遗留问题 由于 JS ES3语法中的 var 提升变量.没有块级作用域,因而 ...

  9. Java动态代码模式

    java动态代理(JDK和cglib) JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委 ...

  10. [C++] String Basic

    Namespace Declarations A using declaration let us use a name from a namespace without qualify the na ...