leetcode 【 Search for a Range 】python 实现
题目:
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]
.
代码:oj测试通过 Runtime: 91 ms
class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return a list of length 2, [index1, index2]
def searchAllTarget(self, A, index, target):
# left index
left_index = index
curr_index = index
while curr_index>=0 and A[curr_index]==target:
left_index = curr_index
curr_index = curr_index-1
# right index
right_index = index
curr_index = index
while curr_index<len(A) and A[curr_index]==target:
right_index = curr_index
curr_index = curr_index+1
return [left_index,right_index] def searchRange(self, A, target):
# none case
if A is None:
return None
# short length cases
if len(A)==1 :
return[[-1,-1],[0,0]][A[0]==target]
# binary search
start = 0
end = len(A)-1
while start<=end :
if start==end:
if A[start]==target :
return self.searchAllTarget(A, start, target)
else :
return [-1,-1]
if start+1==end :
if A[start]==target :
return self.searchAllTarget(A, start, target)
elif A[end]==target :
return self.searchAllTarget(A, end, target)
else :
return [-1,-1]
mid = (start+end)/2
if A[mid]==target :
return self.searchAllTarget(A, mid, target)
elif A[mid]>target :
end = mid-1
else :
start = mid+1
思路:
这道题还是基于binary search,但是要求找到的是某个值的range。
分两步完成:
step1. 常规二分查找到target的某个index;如果没有找到则返回[-1,-1]
step2. 假设A中可能有多个位置为target,则从step1找到的index开始向左右search,直到把index左右两侧的target都找出来。
齐活儿
leetcode 【 Search for a Range 】python 实现的更多相关文章
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- Openfire+spark在linux上搭建内部聊天系统
一. 实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...
- pat甲级1013
1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...
- raspberrypi&linux
Raspberrypi&linux 2018-01-23 19:54:01 Let's go!
- EF和linq语句查询条件不等于某个参数出现的问题
where t.a!=字符串 这是错误的写法,正确为 where t.a!=字符串.trim() 其他类型变量需要保持实体类型和查询条件参数的类型是一致的,不然出现的语句可能会是 类似`Exten ...
- Android(java)学习笔记95:Android运行时异常"Binary XML file line # : Error inflating class"
在原生Android下编译APK,编译没有问题,但是在运行的时候经常出现如标题所描述的异常:"Binary XML file line # : Error inflating class&q ...
- 6.3安装squid
1. Frist you need to install Development tools #yum groupinstall "Development Tools" 2. Ge ...
- Being a Good Boy in Spring Festival(博弈)
Being a Good Boy in Spring Festival Problem Description一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次 ...
- java调用摄像头
http://blog.csdn.net/xing_sky/article/details/43482213 原文地址:http://blog.csdn.net/zajin/article/detai ...
- Load事件中控件Focus()无效解决办法
原因:Load窗体时,窗体未显示 解决:1.Focus()之前添加this.Show(); 2.在Shown事件中添加Focus()
- 自定义配置Webpack和Babel配置
在使用ant-design-vue的包时样式是可以生效的但是如果我需要用到less文件时会报一个异常 当然这个异常其实很清晰的说明了什么问题看错误信息里面有issues地址,看来问题不止我们遇见了可以 ...