LeetCode 34. Search for a Range (找到一个范围)
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 (找到一个范围)的更多相关文章
- [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 ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
随机推荐
- 基于图形检测API(shape detection API)的人脸检测
原文:https://paul.kinlan.me/face-detection/ 在 Google 开发者峰会中,谷歌成员 Miguel Casas-Sanchez 跟我说:"嘿 Paul ...
- Struts2第十篇【数据校验、代码方式、XML配置方式、错误信息返回样式】
回顾以前的数据校验 使用一个FormBean对象来封装着web端来过来的数据 维护一个Map集合保存着错误信息-对各个字段进行逻辑判断 //表单提交过来的数据全都是String类型的,birthday ...
- 详细解读-this-关键字在全局、函数、对象、jQuery中的基础用法!
一.前言 1. Javascript是一门基于对象的动态语言,也就是说,所有东西都是对象,一个很典型的例子就是函数也被视为普通的对象.Javascript可以通过一定的设计模式来实现面向对象的编程,其 ...
- Navicat for MySQL:快捷键整理
使用快捷键,提升工作效率! ctrl+q 打开查询窗口 ctrl+/ 注释sql语句 ctrl+shift +/ 解除注释 ctrl+r 运行查询窗口的sql语句 ctrl+shift+r 只运行选中 ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- H5音频处理的一些小知识
前 言 LiuDaP 十一过后,小编要做一个关于音乐播放器的项目,要用到大量H5音频处理的内容,于是在十月一日国庆黄金周闲暇之际,自己学习了一下H5音频的相关内容.虽然自学的没有那么深入,但是对 ...
- 面向对象编程笔记--static
通过static方法,提供静态的不需要实例化即可访问的方法或属性.所有的调用者可以使用同一个类(不实例化)或对象(只实例化一次),可以应用的场景: 1)各个调用者共享数据,协同工作. 2)对象只可以实 ...
- 三大开源运维监控工具zabbix、nagios、open-falcon优缺点比较
借鉴一下别人的,自己做个记录,我觉得推荐还是使用open-falcon,最重要的一点是有完善的中文帮助文档. 帮助文档地址:https://book.open-falcon.org/zh/index. ...
- Maven打包Jar
现状 该项目使用了Maven,并且引入了Spring,包含代码.配置文件.Jar包,使用的是IDEA来作为开发工具,项目的产出物是要打包成一个可运行的Jar包.通过IDEA的打包工具也可以打包成功,只 ...
- (@WhiteTaken)解决Unity5.x下UnityVS2013不能使用的问题
终于解决了这一困扰我很久的问题. 下面来介绍一下我遇到的问题: 前段时间,重新做了系统,并且安装了Unity5.6版本,VS2013,UnityVS 2013.msi,Visual Studio 20 ...