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 ...
随机推荐
- 用List传递学生信息
集合在程序开发中经常用到,例如,在业务方法中将学生信息.商品信息等存储到集合中,然后作为方法的返回值返回给调用者,以此传递大量的有序数据. 本实例将使用List集合在方法之间传递学生的信息.实例效果如 ...
- MySQL线程池的引入可以提高我们的MySQL的性能
支持线程池的版本:MySQL 企业版本,MySQL percona的分支 MariDB 的版本.我们知道我们的MySQL 语句是不支持硬解析的,没有无SQL 解析 cache.每个连接对应一个线程,我 ...
- MySQL锁和事务(一):InnoDB锁(MySQL 官方文档粗翻)
// 写在前面,实际上,数据库加锁的类型和范围受到多种因素的影响,例如数据库隔离等级,SQL语句,是否使用主键.索引等等.可以查看博文: http://www.cnblogs.com/zhaoyl/p ...
- 基于C#的BarCode 39实现
一.39条码简介 39码是1974年发展出来的条码系统,是一种可供使用者双向扫瞄的分散式条码,也就是说相临两资料码之间,必须包含一个不具任何意义的空白(或细白,其逻辑值为0),且其具有支援文字的能力, ...
- shim 和 polyfill
在前端,有两个词经常被提及:shim 和 polyfill.最近在翻译文章时又遇到了 polyfill 这个词,准备把这两个概念理清楚. 关于 JavaScript 的兼容性问题,通常有不同的解决方案 ...
- tcpip第三章
1,ip协议不可靠.无连接特性介绍 不可靠:计算机A往计算机B发送数据报1,若途径的路由器缓存已满,或者ttl(time to live 生存周期)到了,则路由器直接丢弃数据包1,并产生icmp数据包 ...
- POJ-2417-Discrete Logging(BSGS)
Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N &l ...
- 最接近原生APP体验的高性能前端框架——MUI
前 言 MUI有三大特点: 轻量 追求性能体验,是我们开始启动MUI项目的首要目标,轻量必然是重要特征: MUI不依赖任何第三方JS库,压缩后的JS和CSS文件仅有100+K和60+K 原生UI ...
- 箱线图(boxplot)简介与举例
简述: 盒图是在1977年由美国的统计学家约翰·图基(John Tukey)发明的.它由五个数值点组成:最小值(min),下四分位数(Q1),中位数(median),上四分位数(Q3),最大值(m ...
- ESLint可共享配置发布,团队自定义ESLint规则新鲜出炉
ESLint于2013年6月份推出,至今4个年头,最新版本v4.8.0.它是目前主流的用于Javascript和JSX代码规范检查的利器,很多大公司比如Airbnb和Google均有一套自己的Java ...