题目

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;
}
};
假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30466235
)

[LeetCode] Search for a Range [34]的更多相关文章

  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 搜索一个范围

    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 python

    class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...

  5. [LeetCode] Search for a Range 二分搜索

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  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 (TODO)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. LeetCode Search for a Range (二分查找)

    题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...

随机推荐

  1. XML文件解析之--DOM与SAX

    xml文件又称‘可扩展性标记语言’,可以对文档和数据进行结构化处理,从而能够在部门.客户和供应商之间进行交换,实现动态内容生成,企业集成和应用开发. 我们在进行web开发的时候离不开xml文件,xml ...

  2. javascript 中 事件流和事件冒泡

    一.事件流 是描述页面接受事件的顺序,IE 使用的是时间冒泡流;而Netscape的事件采用的是事件捕获流.1.事件冒泡JS 和 HTML是通过事件的方式实现交互.事件冒泡 开始元素,将事件逐级传递, ...

  3. javascript 回调函数应用

    回调函数是什么在学习之前还真不知道js回调函数怎么使用及作用了,下面本文章把我在学习回调函数例子给各位同学介绍一下吧,有需了解的同学不防进入参考. 回调函数原理: 我现在出发,到了通知你”这是一个异步 ...

  4. Mac Python路径总结

    Mac 下Python 可以多版本的并存,并且Python的目录也有好几个,不过总体来说,Mac 自带的有python 还是比较方便的 Mac 系统自带的又Python ,可能Python版本需要更新 ...

  5. 研究一下TForm.WMPaint过程(也得研究WM_ERASEBKGND)——TForm虽然继承自TWinControl,但是自行模仿了TCustomControl的全部行为,一共三种自绘的覆盖方法,比TCustomControl还多一种

    先擦除背景: procedure TCustomForm.WMEraseBkgnd(var Message: TWMEraseBkgnd); begin if not IsIconic(Handle) ...

  6. Minor GC、Major GC和Full GC之间的区别(转)

    在 Plumbr 从事 GC 暂停检测相关功能的工作时,我被迫用自己的方式,通过大量文章.书籍和演讲来介绍我所做的工作.在整个过程中,经常对 Minor.Major.和 Full GC 事件的使用感到 ...

  7. find: paths must precede expression(转)

    find: paths must precede expressionUsage: find [-H] [-L] [-P] [path...] [expression] 然后就上网查了一下,结果搜索到 ...

  8. uva 12300 - Smallest Regular Polygon

    题意:给定两个点A和B,求包含这两个点的面积最小的正 n(已知)边形. #include<iostream> #include<iomanip> #include<cma ...

  9. 关于Delphi XE2的FMX的一点点研究之消息篇

    Delphi XE2出来了一阵子了,里面比较抢眼的东西,除了VCLStyle这个换肤的东西之外,另外最让人眼亮的应该是FMX这个东西了.万一的博客上都连载了一票的关于FMX的使用心得了.我还是没咋去关 ...

  10. spring mvc controller间跳转 重定向

    1. 需求背景     需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...