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. SAP Sybase IQ 操作基础

    1.启动 source IQ-16_0.sh 命令行查看安装程序是否成功 start_iq -v2 2.数据库.表空间 start_iq -n utility_db dbisql -c 'uid=db ...

  2. 小y的质数

    题目链接:https://ac.nowcoder.com/acm/contest/634/C 链接:https://ac.nowcoder.com/acm/contest/634/C来源:牛客网 题目 ...

  3. 搭建github静态博客

    github设置 建立新的repository,命名为OwnerName.github.io,例如gotochenglong.github.io git管理 设置ssh密匙 使用命令ssh-keyge ...

  4. 《C++ Primer(第五版)》知识巩固

    运行平台:ubuntu 12.04/GCC 4.8.0 第二章:基本内置类型 1.decltype类型指示符 当我们从表达式的类型来推断要定义的类型时,可以使用decltype()来解析:declty ...

  5. Delphi导出word

    //导出Wordprocedure TFrm_Computing.ExportWord;varwordApp, WordDoc, WrdSelection, wrdtable, wrdtable1, ...

  6. DBCP连接池工具类模板

    package ${enclosing_package}; import java.io.InputStream; import java.sql.Connection; import java.sq ...

  7. Android开发环境搭建步骤-【Android】

    本教程是android开发环境在windows下的安装配置,经本人测试完全正确无误.这个教程是史上最详细的android开发环境搭建教程. 工具/原料 Eclipse 3.7.0.Java Jdk6. ...

  8. SpringBoot和SpringCloud区别

    SpringBoot专注于快速方便的开发单个个体微服务.    SpringCloud是关注全局的微服务协调整理治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来,    为各个服 ...

  9. jQuery 整体架构

    不同于 jQuery 代码各个模块细节实现的晦涩难懂,jQuery 整体框架的结构十分清晰,按代码行文大致分为如上图所示的模块. 初看 jQuery 源码可能很容易一头雾水,因为 9000 行的代码感 ...

  10. 企业为什么需要实施BPM?

    背景:众所周知,近几年企业信息化发展迅速,业务管理系统从大到小,数量众多,如ERP,SCM,PLM,CRM,EHR,OA,BI…… 等,企业的管理人员进行管理的主要手段是通过各个业务系统获得各种管理报 ...