[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. ...
随机推荐
- iOS进阶面试题----经典10道
OneV‘s Den在博客里出了10道iOS面试题,用他的话是:"列出了十个应聘Leader级别的高级Cocoa/CocoaTouch开发工程师所应该掌握和理解的技术" . 在这 ...
- AngularJS之WebAPi上传
AngularJS之WebAPi上传(十) 前言 前面一系列我们纯粹是讲AngularJS,在讲一门知识时我们应该结合之前所学综合起来来做一个小的例子,前面我们讲了在MVC中上传文件的例子,在本节 ...
- 弹出框weeboxs 基本属性总结
使用前需包含以下jquery.js.bgiframe.js.weebox.js文件 boxid: null, //设定了此值只后,以后在打开同样boxid的弹窗时,前一个将被自 动关闭 boxclas ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- 待机状态下,服务类型 WCDMA Service type in Idle mode
Services aredistinguished into categories defined in [7]; also the categorisation of cellsaccording ...
- mmtests使用简介
1.简介 mmtests是一个可配置的测试套件,可以被MM开发者用来进行一个常规测试.理想情况下,它可以与LTP,xfstests等测试工具结合起来实现自动化测试. 2.软件组织 run-mmtest ...
- redis 获取key 过期时间
<pre name="code" class="html">127.0.0.1:6379> keys *b4f107c6-e96c-4a1e- ...
- WinRarHelper帮助类
WinRarHelper帮助类 关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩 ...
- 如何利用Win32API取得另一支程式中的ListView內的所有值(RegisterHotKey,ReadProcessMemory,WindowFromPoint和VirtualAllocEx)
http://blog.csdn.net/shuaihj/article/details/6129506
- 不是技术牛人,如何拿到国内IT巨头的Offer(转)
不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic…在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技术屌 ...