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

题意分析:

  本题是在一个递增数组中查找目标值target下标的边界,可以得到两个信息:1.数组会有重复值;2.数组是严格单调递增的。

解答:

  本题要求时间复杂度是 O(log n),提示我们可以用二分查找去做(注意不能先用二分查找找到target,然后向两侧寻找边界,这种方法是不符合时间复杂度要求的)。既然是一个范围,那么我们可以查找两次,分别把两个边界找出来。这里我是先查找到左边界,然后将左边界后面的数组作为新的数组查找右边界。这和二分查找的思想一致,但是需要注意迭代的条件是不同的。

AC代码:

class Solution(object):
def searchRange(self, nums, target):
left = 0
right = r_right = len(nums) - 1
# find the left of range
while left < right:
mid = (left + right) / 2
if nums[mid] < target:
left = mid + 1
else:
right = mid
# can't find target
if nums[left] != target:
return [-1, -1]
while right < r_right:
# notice: mid should be close to right
mid = (right + r_right) / 2 + 1
if nums[mid] > target:
r_right = mid - 1
else:
right = mid
return [left, right]

【LeetCode题意分析&解答】34. 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. 【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 ...

  5. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  6. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. 【LeetCode题意分析&解答】41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  9. 【LeetCode题意分析&解答】39. Combination Sum

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

随机推荐

  1. 使用关联对象(AssociatedObject)为UIButton添加Block响应

    在开发中,要给UIButton添加点击事件的话,通常的做法是这样的 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [ ...

  2. CentOS启动报错:Centos kernel panic-not syncing:VFS:Unable to mount root fs on unknown block

    Centos kernel panic-not syncing:VFS:Unable to mount root fs on unknown block 原因:主要问题就是你更新了内核之后出现的问题. ...

  3. 已经包含了#include <atlcom.h> #include <comutil.h>还是报错

    在WTL工程的.h中 #include <atlbase.h>#include <atlcom.h>#include <atlcomcli.h>#include & ...

  4. 加密传输SSL协议5_Hash Function

    怎么对一个大的文件进行签名,因为文件比较大,非对称签名很慢.那么想,我能把这个大的文件通过一种函数变换,变成一个和源文件唯一对应的的小的文件吗?答案是可以的. Hash Function 这里任何的文 ...

  5. OC语法6——内存管理之引用计数器(retain,release)

    OC内存管理: 一.引用计数器: Java有垃圾回收机制(Garbage Collection,GC).也就是说当我们创建对象后,不需要考虑回收内存的事,Java的垃圾回收机制会自动销毁该对象,回收它 ...

  6. OC语法3——点语法,self关键字

    点语法:   为了给程序员提供便捷,OC中也引入了点语法.不过它和Java中点语法的意义是完全不同的. 在Java中无论调用任何方法,还是访问public类型的成员变量都是用点语法(.号). 而在OC ...

  7. java/php/c#版rsa签名以及验签实现

    本文为转载,请转载请注明地址: 原文地址为        http://xw-z1985.iteye.com/blog/1837376 在开放平台领域,需要给isv提供sdk,签名是Sdk中需要提供的 ...

  8. sass和compass安装

    安装Sass和Compass sass基于Ruby语言开发而成,因此安装sass前需要安装Ruby.(注:mac下自带Ruby无需在安装Ruby!) window下安装SASS首先需要安装Ruby,先 ...

  9. B/S结构和C/S结构

    概念: C/S结构,即Client/Server(客户机/服务器)结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,可以充分利用两端硬件环境 ...

  10. Latex笔记-基本布局

    转自https://linhan.blog.ustc.edu.cn/?p=135&cpage=1 目录改用中文标题并且居中 \renewcommand{\contentsname}{\cent ...