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].

解题思路:

典型的二分查找应用,此题读透,可以基本掌握二分查找的使用;

二分查找主要需要注意以下几个方面:

(1)如何设置循环条件(l<r 或 l <= r);

(2)如何设置mid;

(3)比较mid后,如何更新l 和 r;

(4)循环结束后,l或r谁是要寻求的正确值;

二分查找思路总结:

1、确定循环范围的原则

  必须保证计算的范围内,包含要寻找的值;

2、保证每次循环的更新步长最小为1

  循环移动的最小步长可能为1,也可能为2。(为2的情形:mid = (l+r)/2,即mid>=l,mid<r,而更新时选择,r = mid - 1,造成每次更新r时,步长至少为2)

  始终选择步长为1,因为步长为2逻辑思路不如步长1清晰,并且步长2在循环跳出时,对l和r的状态不确定,容易漏判边界条件;

3、在步长为1时,始终使用l<r

  因为,当步长为1,l<r跳出循环,一定是以l=r的形式跳出的,即只要保证l或r肯定有一个满足要求,便不必考虑l=r的情况。

4、步长为1的两种情况

  (1)如果mid是(l+r)/2,那么mid可能等于l,因此每次更新l必须为mid+1;r始终大于mid,因此更新r时,选择r=mid;

  (2)如果mid是(l+r)/2 + 1,那么mid可能等于r,因此每次更新r,必须为mid-1;l始终小于mid,因此更新时,选择l=mid;

  遇到具体问题时,一般根据实际需要倒推,比如需要每次l = mid+1,则使用(1);需要r = mid - 1,则使用(2)。

5、循环结束的返回值

  在步长为1且l<r的条件下,循环结束时l=r,因此返回哪个都正确;

  但是如果步长和条件改变,则要根据情况变化;

对于本题:

1、先用二分法找左边界

  找左边界时,应该保证所查范围内,一定包含左边界,因此当mid=target时,r应该=mid,而不是mid-1,因此可以使用策略4(1);

2、再用二分法找右边界

  找右边界时,应该保证所查范围内,一定包含右边界,因此当mid=target时,l应该=mid,而不是mid+1,因此不能使用策略4(1),要使用策略4(2);

3、l和r跳出循环时相等,因此判断/返回哪一个都行;

代码:

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) { int n = nums.size();
vector<int> ret = {-, -}; int l = ;
int r = n - ;
while (l < r) {
int mid = (l + r) / ;
if (nums[mid] >= target)
r = mid;
else
l = mid + ;
}
if (nums[r] != target)
return ret;
ret[] = r; r = n - ;
while (l < r) {
int mid = (l + r) / + ;
if (nums[mid] > target)
r = mid - ;
else
l = mid;
}
ret[] = l; return ret;
}
};

  

【Leetcode】【Medium】Search for a Range的更多相关文章

  1. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode题意分析&解答】33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  6. 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...

  7. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. 【leetcode刷题笔记】Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  9. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  10. 【leetcode刷题笔记】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. 修改eclipse下tomcat的内存大小/解决内存溢出

    我们安装完成eclipse之后,在我们的安装目录下有一个名为eclipse.ini文件. 打开文件里面的内容如下: -startup plugins/org.eclipse.equinox.launc ...

  2. 【随笔】node.js + npm的安装

    需要用到node.js和npm,所以来安装下. 在网上找了找教程,好多都是分开装的,各种麻烦各种事,最后还是在node.js官网里下载解决了.记录一下. 如果安装在当前环境,直接点击install会自 ...

  3. WPF的RadioButton--单选框

    1. 使用, 显示的内容改为Content属性 <RadioButton Content="boy"/> 2. 要使用分组,就是用 GroupName属性 <Ra ...

  4. TCP连接管理(TCP Connection Management)

    在最近的求职面试过程中,关于"建立TCP连接的三次握手"不止一次被问到了,虽然我以前用同样的问题面试过别人,但感觉还是不能给面试官一个很清晰的回答.本文算是对整个TCP连接管理做一 ...

  5. Vue父子组件生命周期执行顺序及钩子函数的个人理解

    先附一张官网上的vue实例的生命周期图,每个Vue实例在被创建的时候都需要经过一系列的初始化过程,例如需要设置数据监听,编译模板,将实例挂载到DOM并在数据变化时更新DOM等.同时在这个过程中也会运行 ...

  6. sublime text 3支持GBK编码

    1.安装Package Control: 按Ctrl+~打开命令行,然后复制粘贴下面这一行代码,回车确定: import urllib.request,os; pf = 'Package Contro ...

  7. Layer UI 模块化的用法(转)

    此文章适合入门的同学查看,之前因为项目的原因,在网上找了一套Layer UI做的后台管理系统模板,完全不懂LayUI里面的JS用法,看了官方文档和其它资料后才明白怎么去实现模块化这个例子,但是还是感觉 ...

  8. 01.MD5加密

    namespace _01.MD5加密 { class Program { static void Main(string[] args) { //MD5加密就是给想要的密码或者其它字符加密 //如果 ...

  9. 二:SpringAOP

    一:AOP 面向切面编程思想 横向重复,纵向抽取 |- filter中 |- 动态代理 |- interceptor中 二:动态代理 1.通过动态代理可以体现aop思想 2.对目标对象中的方法进行增强 ...

  10. Excel批量生成条形码

    项目要求需要将信息做成条形码的形式,以便通过手持设备直接查看信息,因本次项目信息为固定信息,不需随机生成,故采用本方法: 代码随机生成二维码暂时没有接触,后续有时间会研究 步骤: 1.新建 Excel ...