[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 ...
随机推荐
- redis 类型、方法
之前使用redis,现在对所有redis方法做一个总结: string类型 形式:key=>value; 说明:最简单的类型:一个key对应一个value,value保存的类型是二进制安全的,s ...
- vue2组件之间双向数据绑定问题
最近在使用element-ui的dialog组件二次封装成独立组件使用时,子组件需要将关闭dialog状态返回给父组件,简单的说就是要实现父子组件之间的数据双向绑定问题. 大致代码如下: 1,父组件 ...
- CentOS安装JMeter
mkdir /usr/local/jmeter 新建jmeter目录 cd /usr/local/jmeter 进入jmeter目录 wget https://archive.apache.org/d ...
- Unity与服务区交互数据
Unity与服务区交互数据 Unity可能在用的时候使用到登陆等需要与服务器交互数据.今天尝试使用了WWW类和WWWForm类来实现Get请求与Post请求. 1.WWW Unity圣典解释: WWW ...
- Java学习 · 初识 IO流
IO流 1. 原理与概念 a) 流 i. 流动,流向 ii. 从一端移动到另一端 源头到目的地 iii. 抽象.动态概念,是一连 ...
- 【转载】Android 内存溢出如何发生的。
[转载]Android 内存溢出如何发生的. 且谈Android内存溢出 前言 关于android的内存溢出在创新文档库中也有不少,网络上也有很多这方面的资料.所以这遍文章不算是正真意义上的创新,仅仅 ...
- Pandas dataframe数据写入文件和数据库
转自:http://www.dcharm.com/?p=584 Pandas是Python下一个开源数据分析的库,它提供的数据结构DataFrame极大的简化了数据分析过程中一些繁琐操作,DataFr ...
- URAL 1519 Formula 1(插头DP,入门题)
Description Background Regardless of the fact, that Vologda could not get rights to hold the Winter ...
- iOS- Swift实现UITableView的常见操作
1.前言 Swift在这就不多介绍了,想必大家都已皆知. 离Swift面世也过了有一个多月的时间. 在闲暇时间我用Swift实现了UITableView的一些常见操作. 基本都是可以用上的,今天在 ...
- iOS开发热更新JSPatch
JSPatch,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的原生接口,获得脚本语言的能力:动态更新APP,替换项目原生代码修复bug. 是否有过这样的经历 ...