题目描述:

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. Flyme适配源码更新命令,轻松完成打包

    第一次已经同步了所有源码(花了很长时间),第一次已经连接手机进行了插桩和解reject,那么第二次还需要这么麻烦吗?答案是:NO ! 1.官方源码,执行如下命令可以实现: repo sync -c 2 ...

  2. 精通Web Analytics 2.0 (5) 第三章:点击流分析的奇妙世界:指标

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第三章:点击流分析的奇妙世界:指标 新的Web Analytics 2.0心态:搞定它.新的闪亮系列工具:是的.准备好了吗?当然 ...

  3. Honeywords项目——检查密码是否被破解的一种简单方法

    Honeywords项目使用一种简单的方法来改进hash后的密码的安全性——为每个账户维护一个额外的honeywords(假密码).如果有黑客拿到了密码的文件,然后试图用brute froce的方式破 ...

  4. POJ2417 Discrete Logging

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. Photon服务器进阶&一个新游戏的出产(二)

    继续上个文章说~ 接收其他人发过来的广播,在OnEvent中进行响应 比如说接收过来加入的消息 public void OnEvent(EventData eventData) { Debug.Log ...

  6. 在WildFly中运行多个standalone模式的实例

      WildFly作为一款优秀的EJB容器,其前身为JBoss AS.JBoss作为一款开源的应用服务器,被广泛的应用在各种项目当中.假设我们现在有这样一个项目,他是以standalone的模式运行在 ...

  7. excel学习

    制作打钩方格 :输入R ,然后选中R,在字体中选中wingdings 2即可 快速选中一列:选中两行以上,然后Ctrl+Shift+下 分段显示手机号:选中单元格,Ctrl+1,然后选中数字,自定义, ...

  8. 【转载】Python与ArcGIS Engine的集成

    本文转载自Fransico<Python与ArcGIS Engine的集成>   1 在Python中调用AO类库 1.1  准备工作 本文所使用环境:ArcGIS 10.0.Python ...

  9. Python学习笔记——元组

    1.创建一个元组并给它赋值 >>> aTuple = (123,'abc',4.56,['inner','tuple'],7-9j) >>> aTuple (123 ...

  10. [Nhibernate]SchemaExport工具的使用(一)——通过映射文件修改数据表

    目录 写在前面 文档与系列文章 SchemaExport工具 SchemaUpdate工具 一个例子 总结 写在前面 上篇文章介绍了使用代码生成器的nhibernate模版来生成持久化类,映射文件等内 ...