[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. ...
随机推荐
- Qt调用摄像头(截取并保存图片)
原地址:http://blog.csdn.net/liang19890820/article/details/12782531 Qt如何调用系统摄像设备进行显示.截图.录制? QCamera: ...
- 微信公 众平台开发,用于个人技术交流,有兴趣的加QQ群432921500
微信公 众平台开发,用于个人技术交流,有兴趣的加QQ群432921500
- Delete it
Problem A: Delete it Time Limit: 2 Sec Memory Limit: 64 MB Submit: 99 Solved: 25 Description 克林在 ...
- hbase memstorelab
关于MemStore的补充 在通过HStore.add向store中加入�一个kv时,首先把数据写入到memstore中.这一点没有什么说明: publiclongadd(finalKeyValue ...
- C语言sendto()函数-经socket传送数据以及recvfrom函数《转》
相关函数:send, sendmsg, recv, recvfrom, socket 头文件:#include <sys/types.h> #include <sys/socke ...
- Jetty支持Windows认证
WAFFLE是什么? Jetty增加WAFFLE支持 DEMO 小结 WAFFLE是什么? WAFFLE是一个Windows认证框架,支持Negotiate, NTLM和Kerberos认证.WAFF ...
- Lucene.Net 2.3.1开发介绍 —— 二、分词(三)
原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(三) 1.3 分词器结构 1.3.1 分词器整体结构 从1.2节的分析,终于做到了管中窥豹,现在在Lucene.Net项目中添加一个类关 ...
- Lucene.Net 2.3.1开发介绍 —— 二、分词(一)
原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(一) Lucene.Net中,分词是核心库之一,当然,也可以将它独立出来.目前Lucene.Net的分词库很不完善,实际应用价值不高.唯 ...
- java中遍历MAP,嵌套map的几种方法
java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>(); map.put("us ...
- URL加随机数的作用
原文:URL加随机数的作用 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的时候总是使用IE缓存,为了解决这个问题一般可以用一下方法: 1 ...