Given an array of integers sorted in ascending order, 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].


题目标签:Array

  这道题目给了我们一个 有序序列,其中是有重复的,让我们找到target的范围,如果没有target,就返回 [-1,-1]。既然规定我们用O(log n),那么肯定是利用binary search。问题是,用binary search 是可以找到target, 但是这里需要找到一个范围,中间可能有很多个重复的target。我们可以用两次binary search, 第一次就找到第一个target, 第二次找到最后一个target,这样就可以返回target的范围了。

  怎么来找到第一个target呢,分析一下,如果中间的数字 大于 target, 那么说明target 还在更左边, 我们要把 right = mid - 1。如果中间数字 小于 target, 说明target 在更右边,要把 left = mid + 1。 到这里为止,都是普通的二分法,那么如果中间的数字是等于 target 的呢,因为我们要找到第一个target的位置,所以,就算找到了target, 我们也要向左边移动,因此,要把这个等于的情况加入 大于里面去。因为它们两种情况都是向左边移动。每次找到target 的时候,还需要 记住它的位置,之后一直更新。因为你找到的target 不一定是最左边的,所以要一直更新,最后一次就是最左边的target位置。

  找最后一个target也是同理。

Java Solution:

Runtime beats 79.19%

完成日期:07/14/2017

关键词:Array

关键点:用两次二分法,第一次来找最左边的target,第二次来找最右边的target

 public class Solution
{
public int[] searchRange(int[] nums, int target)
{
int[] res = {-1, -1}; // first binary search to find the first target
int left = 0;
int right = nums.length - 1; while(left <= right)
{
int mid = left + (right - left) / 2;
if(nums[mid] >= target) // if mid one is greater or equal to target
right = mid - 1; // move to left half
else // if mid one is less than target
left = mid + 1; // move to right half if(nums[mid] == target) // update index
res[0] = mid;
} // second binary search to find the last target
right = nums.length - 1; while(left <= right)
{
int mid = left + (right - left) / 2;
if(nums[mid] <= target)
left = mid + 1;
else
right = mid - 1; if(nums[mid] == target)
res[1] = mid;
} return res;
}
}

参考资料:

https://leetcode.com/problems/search-for-a-range/#/discuss

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 34. Search for a Range (找到一个范围)的更多相关文章

  1. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  2. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  3. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  4. leetcode 34 Search for a Range(二分法)

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  5. leetcode@ [34] Search for a Range (STL Binary Search)

    https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...

  6. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  7. [leetcode 34] search for a range

    1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  8. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  9. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

随机推荐

  1. 多线程面试题系列(2): CreateThread与_beginthreadex本质区别

    本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_beginthreadex的本质区别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...

  2. Apache Spark 2.2.0 中文文档 - 概述 | ApacheCN

    Spark 概述 Apache Spark 是一个快速的, 多用途的集群计算系统. 它提供了 Java, Scala, Python 和 R 的高级 API,以及一个支持通用的执行图计算的优化过的引擎 ...

  3. IIS部署新网站

    Windows Server使用IIS 6.0配置ASP动态Web网站 http://jingyan.baidu.com/article/c1a3101ee43ae9de656debb4.html h ...

  4. java最全时间类及用法

    对于时间类,这篇主要说明各种现实情况下如何取值,怎么定向取值,得到自己想要的时间参数.在java中时间类主要有Date.Calendar,暂时只介绍 java.util.*下的时间类,对于java.s ...

  5. bzoj 3212 Pku3468 A Simple Problem with Integers

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MB Description You ...

  6. SpringAop详解

    近几天学习了一下SpringAop在网上找了一些资料,此链接为原文链接http://www.cnblogs.com/xrq730/p/4919025.html AOP AOP(Aspect Orien ...

  7. [js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

    接着上文线条样式[js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)继续. canvas提供两种输出文本的方 ...

  8. 理解ES6——Promise

    浏览器的控制台真是个好东西,啥都能干: 这就是Promise,能看出来啥? 1.是个构造函数,可以new实例. 2.自身有一些方法:all.race.reject.resolve... 3.原型上有c ...

  9. Java Byte取值范围

    Java Byte 的取值范围大家都知道(-128 ~ 127),那么-128 和 127 这两个数是怎么计算的呢? #大学知识回顾: 概念:负数的补码是该 数 绝 对 值 的 原 码 按 位 取 反 ...

  10. 关于select的一个错误---属性选择器

    错误: jquery 获取下拉框 text='1'的 option 的value 属性值  我写的var t= $("#selectID option[text='1']).val() ; ...