题目描述:

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].

解题思路:

明显的简单二分问题,首先用二分找到一个满足条件的点,然后向两边延展即可

coding=utf-8

class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return a list of length 2, [index1, index2]
def searchRange(self, A, target):
l = len(A)
left = 0
right = l-1
index1 = -1
index2 = -1
pos = -1
while left <= right:
mid = (left + right) / 2
if A[mid] == target:
pos = mid
break
elif A[mid] > target:
right = mid - 1
else:
left = mid + 1
#print pos
if pos == -1:
return [-1,-1]
index1 = index2 = pos
while A[index1] == target and index1 > 0 and A[index1-1] == target:
index1 -= 1
while A[index2] == target and index2 < l-1 and A[index2+1] ==target:
index2 += 1
return [index1,index2] s = Solution()
a = [5, 7, 7, 8, 8, 10]
print s.searchRange(a,8)
a = [1]
print s.searchRange(a,1)

【leetcode】Search for a Range的更多相关文章

  1. 【leetcode】Search for a Range(middle)

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

  2. 【LeetCode】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 ...

  3. 【Leetcode】【Medium】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 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 ...

  5. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  6. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  7. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  8. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. 【leetcode】Search in Rotated Sorted Array (hard)

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. c# WebClient文件下载

    public void HttpDownload(string url, string path, ResourceType type) { using (var client = new WebCl ...

  2. Python 从零学起(纯基础) 笔记 之 collection系列

    Collection系列  1.  计数器(Counter) Counter是对字典类型的补充,用于追踪值的出现次数   ps  具备字典所有功能 + 自己的功能 Counter import col ...

  3. 运维请注意:”非常危险“的Linux命令大全

    Linux命令是一种很有趣且有用的东西,但在你不知道会带来什么后果的时候,它又会显得非常危险.所以,在输入某些命令前,请多多检查再敲回车. rm –rf rm –rf是删除文件夹和里面附带内容的一种最 ...

  4. <<< 判断提交方式是get还是post

    if("GET".equals(request.getMethod())){ System.out.println("提交方式是GET"); }else if( ...

  5. centos上如何安装redis?|centos傻瓜式安装redis教程

    本文介绍centos安装redis,请不要安装2.4.3,是有问题的. 首先安装gcc yum -y install gcc yum -y install gcc-c++ yum install ma ...

  6. 数据库性能优化常用sql脚本总结

    最近闲来无事,正好抽出时间,来总结总结 sql性能优化方面的一下小技巧,小工具.虽然都是些很杂的东西,但是我个人觉得,如果真的清楚了里面的一下指标,或许真的能抵半个DBA. 有些时候,找不到DBA或者 ...

  7. 收藏一些好用的fifo

    1.Nordic库中的 E:\nRF52_SDK_0.9.2_dbc28c9\components\libraries\fifo app_fifo.c /* Copyright (c) 2013 No ...

  8. ubuntu 系统使用

    1.ubuntu的鼠标,用起来总是感觉比windows的快一点儿,可以用以下命令来调整为默认的 root@admin-pc:~$ xset m default 2.mysql默认不允许远程连接,可以在 ...

  9. [Head First设计模式]餐馆中的设计模式——命令模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  10. ActiveMQ结合Spring开发

    ---------------------------------------------------------------------------- Spring结合ActiveMQ开发 : 发送 ...