题目

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. 使用kvm制作Eucalyptus镜像(CentOS 6.5为例)

    1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...

  2. Spring mvc + maven + tomcat配置问题

    在用maven搭建spring mvc时候, 个人遇到过很多的问题, 现在把遇到的问题总结下: 1.  首先点击项目->Run As->Maven clean, 这一步把之前不管有没有ma ...

  3. C#Udp组播

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  4. orbbec astra ros

    install instruction: https://www.ncnynl.com/archives/201703/1444.html problem: [ERROR] [1481169521.4 ...

  5. 搭建zabbix服务器监控

    搭建zabbix 监控服务 服务器环境Centos 7.3 修改网卡名称 高并发优化 Web环境 nginx + php-fpm 必须对nginx配置有连接优化 使用systemd服务启动nginx和 ...

  6. oracle数据类型及操作

    1. Oracle字符串操作 1.1 字符串类型 Ø CHAR和VARCHAR2类型 l CHAR存放定长字符,如果数据存不满指定长度则用空格补齐,CHAR类型浪费空间换取查询时间的缩短. l VAR ...

  7. 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案

    更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...

  8. idea字体模糊

    用jdk1.8的jre替换idea的jre64,但是记得在lib里加上jdk的lib中的tools.jar. 如图: 然后 将原来jre64的TOOLS.jar拷贝到替换后的jre的lib目录下,重启 ...

  9. 部署git服务器(Windows Server 2008)

    原来的这个项目是一个人开发的,没有做版本管理,我接手后准备搭建git版本管理服务端,方便离线开发和做版本管理: 一台云主机,操作系统:Windows Server 2008,64位: java已经安装 ...

  10. 【转】浅谈Node.js单线程模型

    Node.js采用 事件驱动 和 异步I/O 的方式,实现了一个单线程.高并发的运行时环境,而单线程就意味着同一时间只能做一件事,那么Node.js如何利用单线程来实现高并发和异步I/O?本文将围绕这 ...