题目

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 实现的更多相关文章

  1. leetcode Search for a Range python

    class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...

  2. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

  3. [LeetCode] Search for a Range 搜索一个范围

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. [LeetCode] Search for a Range(二分法)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  5. leetcode:Search for a Range(数组,二分查找)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  6. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

  7. [LeetCode] Search for a Range 二分搜索

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  8. Leetcode Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. leetcode -- Search for a Range (TODO)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  10. [LeetCode] Search for a Range [34]

    题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...

随机推荐

  1. Eucalyptus-利用镜像启动一个Windows Server 2008r2实例

    1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...

  2. Shape详解

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  3. python super用法

    普通继承 class FooParent(object): def __init__(self): self.parent = 'I\'m the parent.' print 'Parent' de ...

  4. 使用CreateProcess函数运行其他程序

    为了便于控制通过脚本运行的程序,可以使用win32process模块中的CreateProcess()函数创建一个运行相应程序的进程.其函数原型如下.CreateProcess(appName, co ...

  5. 笔记 Activator.CreateInstance(Type)

    这段代码取自NopCommerce 3.80 的 权限列表初始化代码 dynamic provider = Activator.CreateInstance(providerType);   文件位置 ...

  6. linux 命令——25 linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...

  7. POJ-2135 Farm Tour---最小费用最大流模板题(构图)

    题目链接: https://vjudge.net/problem/POJ-2135 题目大意: 主人公要从1号走到第N号点,再重N号点走回1号点,同时每条路只能走一次. 这是一个无向图.输入数据第一行 ...

  8. TypeScript 编译选项

    编译选项 选项 类型 默认值 描述 --allowJs boolean false 允许编译javascript文件. --allowSyntheticDefaultImports boolean m ...

  9. 1.redis 安装

    1.https://redis.io/download. 2. $ wget http://download.redis.io/releases/redis-3.2.9.tar.gz $ .tar.g ...

  10. 【luogu P5022 旅行】 题解

    题目连接:https://www.luogu.org/problemnew/show/P5022 \(NOIP2018 DAY2T1\) 考场上只写了60分,很容易想到当 m = n - 1 时的树的 ...