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. ...
随机推荐
- silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
asp.net开发中,数据绑定是一个很简单的概念,控件与数据绑定后,控件可以自动把数据按一定的形式显示出来.(当然控件上的值改变后,可以通过提交页面表单,同时后台服务端代码接收新值更新数据) silv ...
- 「转」sqlalchemy 0.9.8 多对多级联删除
转自知乎 http://www.zhihu.com/question/21050551 有 A,B,C,AB,AC五张表 class A(base): __tablename__ = "a& ...
- [Hack] 搭建渗透测试实验环境
安装虚拟机镜像,镜像如下: Kali-Linux-2016.1-vm-amd64(https://www.kali.org/) Metasploitable2-Linux(https://source ...
- World Wind Java开发之四——搭建本地WMS服务器(转)
在提供地理信息系统客户端时,NASA还为用户提供了开源的WMS Server 服务器应用:World Wind WMS Server.利用这个应用,我们可以架设自己的WMS服务并使用自己的数据(也支持 ...
- 3218: 字符串字符统计—C语言
3218: 字符串字符统计—C语言 时间限制: 1 Sec 内存限制: 128 MB提交: 270 解决: 129[提交][状态][讨论版][命题人:smallgyy] 题目描述 编写一函数,由实 ...
- mahout算法解析
1.下载ubuntu的iso 2.安装虚拟机,vmware,最好英文原版 3.在vm里面安装ubuntu,安装vm tools 4.本地连接设置为共享上网,虚拟机采用NAT,打开ubuntu,自动连接 ...
- Java基础面试题:super.getClass().getName() 执行结果是什么?
package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...
- js中的||、&&与!用法
&&和||在JQuery源代码内尤为使用广泛,由网上找了些例子作为参考,对其用法研究了一下: 1. && function a(){ alert("a&quo ...
- yum 安装percona mysql 5.7
Mysql5.7安装准备 1.基础信息: (1)可参考官方文档[https://www.percona.com/doc/percona-server/5.7/installation/yum_repo ...
- mysql安装记录
一.创建mysql用户 useradd mysql 二.解压 tar -zxvf mysql-5.6.38.tar.gz 三.安装依赖包 yum install -y ncurses-devel li ...