[LeetCode] Search for a Range [34]
题目
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(n)。可是对于有序数组我们一般能够使用二分查找能够得到更好的O(logn)的时间复杂度。我们能够使用二分查找找到这个数第一次出现的位置和这个数最后一次出现的位置,这样就能够得到它出现的区间。
代码实现
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> ret;
if(A==NULL || n<=0) return ret;
int first = getFirst(A, n, target);
int last = getLast(A, n, target);
ret.push_back(first);
ret.push_back(last);
return ret;
}
int getFirst(int A[], int n, int target){
int begin = 0, end = n-1;
int mid;
while(begin<=end){
int mid = (begin+end)/2;
if(A[mid] == target){
if(mid==0 || A[mid-1]<A[mid])
return mid;
else
end = mid-1;
}else if(A[mid] < target)
begin = mid+1;
else
end = mid-1;
}
return -1;
}
int getLast(int A[], int n, int target){
int begin = 0, end = n-1;
int mid;
while(begin<=end){
int mid = (begin+end)/2;
if(A[mid] == target){
if(mid==n-1 || A[mid+1]>A[mid])
return mid;
else
begin = mid+1;
}else if(A[mid] < target)
begin = mid+1;
else
end = mid-1;
}
return -1;
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Search for a Range [34]的更多相关文章
- 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 搜索一个范围
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 ...
- LeetCode Search for a Range (二分查找)
题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- java泛型接口详解
/* * 泛型接口 */ interface Tool<T> { public void show(T t); //泛型方法 public <E> void print(E e ...
- WCF随笔3----消息编码器
原文:WCF随笔3----消息编码器 我们都知道,message是wcf通信框架进行通信的最基本的单位,但是wcf开发人员其实根本不需要直接与message打交道,一样能够写好wcf相关的程序.这是因 ...
- JavaScript判断横屏/竖屏
/判断手机横竖屏状态: function hengshuping(){ if(window.orientation==180||window.orientation==0){ ...
- PHP移动互联网开发笔记(5)——文件的上传下载
原文地址:http://www.php100.com/html/php/rumen/2014/0326/6706.html 一.文件的上传 1.client设置: (1).在 标签中将enctype和 ...
- CppCMS URL使用
Artyom觉得URL分为三个组成部分: Script_Name / Path_Info ? Query_String 比方以下的: /foo/bar.php/test?x=10 Script_Nam ...
- Object-c @property的用法
property是一种代码生成机制,可以生成不同类型的getter/setter函数,特别是假设你想要用点(.)操作符号来存取变量的话,你就能必须使用property. 怎样使用? 使用方法如:@pr ...
- C# - 使用 List<> 泛型给GridView控件数据
创建实体模型: namespace Test.Models { public class Student { public string ID { get; set; } public string ...
- HDOJ 4883 TIANKENG’s restaurant
称号: TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Ja ...
- .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...
- 【OpenMesh】Some basic operations: Flipping and collapsing edges
这一节中你将学到一些OpenMesh中早已提供的基础操作. 内容包括三角形网格边的翻转以及通过连接邻接的顶点边缘折叠. 三角形网格的翻转(Flipping edges) 考虑到两个邻接面的三角形网格中 ...