【LeetCode题意分析&解答】34. Search for a Range
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的更多相关文章
- 【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 ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- poj2478--欧拉函数打表
此题中对时间有要求,如直接使用欧拉函数求解,每输入一个n,就得进行循环求出<n的每个数的欧拉函数, 这样会超时, 于是我们可预先将欧拉函数打表, 再进行一个循环加法运算,便可不超时得解. #in ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...
- SQLServer 中实现类似MySQL中的group_concat函数的功能
SQLServer中没有MySQL中的group_concat函数,可以把分组的数据连接在一起. 后在网上查找,找到了可以实现此功能的方法,特此记录下. SELECT a, stuff((SELECT ...
- sersync+inotify实时备份数据
Sersync项目简介与框架 简介 Sersync项目利用inotify与rsync技术实现对服务器数据实时同步的解决方案,其中inotify用于监控sersync所在服务器上文件系统的事件变化,rs ...
- 常用DOM整理
常用DOM整理 前言: html为document搭建了一棵DOM树,这棵树就是有一系列Node节点所构成的.他为我们定义了文档的结构. Node类型: Node.ELEMENT_NODE(1); ...
- c语言中的制表符\t与空格
(本文不讨论制表符与空格缩进问题) 编程过程中,我们常常用多个空格或制表符分隔两个字符串,那么这两个在显示效果上有什么区别呢? 比较如下两行代码的输出效果 代码1: printf("1\t1 ...
- linux----定义命令别名
1.定义命令别名的语法: alias nickName='command'#用于定义. unalias nickName#用于撤消一个别名的定义. 如:alias cls='clear' 2.应该要 ...
- CentOS下重新安装yum
1,下载最新的yum-3.2.28.tar.gz并解压 #wget http://yum.baseurl.org/download/3.2/yum-3.2.28.tar.gz#tar xvf yum- ...
- 优盘(U 盘) 采用TLC, MLC, SLC芯片 的区别 与使用寿命
最近一直在看大家在讨论sandisk,pny,金士顿等大厂都开始用tlc的芯片问题,让大家基本都不敢用U盘存数据了按照之前的擦写参数TLC 1000次MLC 10000次SL ...
- delphi程序设计之底层原理
虽然用delphi也有7,8年了,但大部分时间还是用在系统的架构上,对delphi底层还是一知半解,今天在网上看到一篇文章写得很好,虽然是07年的,但仍有借鉴的价值. 现摘录如下: Delphi程序设 ...