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. Python - 购物车代码 (账户登陆,用户个人清单存取,重要信息高亮显示)

    需要掌握open函数. 清单存取时,需要注意编码问题,直接在读取和存储时加上encoding = 'utf - 8' 可以解决gbk - unicode转化时出现的乱码问题. 码农一定要坚强,这份代码 ...

  2. (转)虚拟路由器冗余协议【原理篇】VRRP详解

    原文:http://blog.51cto.com/zhaoyuqiang/1166840 为什么要使用VRRP技术 我们知道,为了实现不同子网之间的设备通信,需要配置路由.目前常用的指定路由方法有两种 ...

  3. linux install tomcat

    折腾了好久,按照官网的安装流程安装了不止3次,发现还是不能成功,最终发现是linux机器本身的问题,因为我用的公司的virtual machine,可能是机器本身在一次迁移的过程当中出现了问题,导致了 ...

  4. sencha touch overlay 里使用 list

    1 sencha touch 中 list 如果不设置一个固定高度或 flex : 1,  list 的内容就不会显示出来. 主要是因为 list 是可滚动的,你不设置高度 ,list 的高度默认就是 ...

  5. Struts2页面配置和访问servlet API

    一.Struts2页面配置 在struts2中页面可以分为两种,全局页面和局部页面. 1.全局页面: 在一个<package></package>标签内的多个action都要跳 ...

  6. nodejs中cookie、session的使用

    因为http会话的无状态性,为了标记用户的登录状态,便出现了cookie.cookie分为很多种,有普通cookie.签名cookie.json cookie等,这里主要记录下在express应用中如 ...

  7. Struts2 学习(二)

    一.Struts2 配置文件 1.配置多个配置文件 在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿. 为了避免struts.xm ...

  8. MongoDB 学习(一)安装配置和简单应用

    一.安装和部署 1.服务端安装 1.官网下载(官方网站 https://www.mongodb.org/downloads/#production),傻瓜式安装,注意修改安装路径. 安装完成后的目录结 ...

  9. Mysql根据经纬度筛选数据

    创建位置表,并且插入测试数据 /*     Navicat MySQL Data Transfer     Source Server         : localhost     Source S ...

  10. javaweb带属性的自定义标签

    带属性的自定义标签: 1.先在标签处理器中定义setter方法,建议把所有的属性类型都设置为String类型. package com.javaweb.tag; import java.io.IOEx ...