【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 ...
随机推荐
- C#去掉字符串中的汉字
string str = "测试一下ilove中国so结束"; Regex reg = new Regex(@"[\u4e00-\u9fa5]"); Label ...
- Excel 提供数据 更新或者插入数据 通过函数 自动生成SQL语句
excel 更新数据 ="UPDATE dbo.yt_vehicleExtensionBase SET yt_purchase_date='"&B2&"' ...
- url的4种访问方式
1.PATHINFO 模式 -- 重点!!!!!! http://域名/项目名/入口文件/模块名/方法名/键1/值1/键2/值2 (可以修改 'URL_PATHINFO_DEPR'=>'-',/ ...
- OC中对象拷贝概念
OC中的对象拷贝概念,这个对于面向对象语言中都会有这种的问题,只是不同的语言有不同的解决方式:C++中有拷贝构造函数,Java中需要实现Cloneable接口,在clone方法中进行操作.但是不过OC ...
- poj1284--原根的性质
关于原根,在百度百科上有着详细的介绍,此题主要考查原根的两个性质 1.只有奇素数才有原根. 2.一个数的原根个数为其欧拉函数的欧拉函数. 综合以上特点,可得到,我们设输入数为n,那么输出结果就为n-1 ...
- Struts2+Ajax实现检测用户名是否唯一
搞了慢慢两天,终于弄明白了怎么在Struts2框架中使用Ajax检测用户名的存在了.虽然,比起那些大牛们来,这速度确实够慢的,不过,最终弄出来还是满满的成就感啊. 闲话休提,言归正传.直接上代码: A ...
- leetcode Binary Tree Paths python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- shell脚本定时备份数据库
脚本代码: 新建文件back_db.sh #!/bin/bash TODAYTIME="`date +%Y%m%d`" DBNAME="test mysql" ...
- Webpack 从0开始
Webpack Demos https://github.com/ruanyf/webpack-demos Docs https://webpack.github.io/docs/?utm_sourc ...
- Python调用C/C++动态链接库的方法详解
Python调用C/C++动态链接库的方法详解 投稿:shichen2014 这篇文章主要介绍了Python调用C/C++动态链接库的方法,需要的朋友可以参考下 本文以实例讲解了Python调用C/C ...