题目描述:

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#-WinForm-Winform TextBox中只能输入数字的几种常用方法(C#)

    方法一: private void tBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 0x20) e.KeyCh ...

  2. 关于背景图相对父容器垂直居中问题 —— vertical-align 和 line-height 之间的区别

       html css <div class="register-wrapper"> <div class="register"> &l ...

  3. 使用Spring进行统一日志管理 + 统一异常管理

    http://blog.csdn.net/king87130/article/details/8011843原文地址 统一日志异常实现类: 1 package com.pilelot.web.util ...

  4. How to setup vsftpd FTP file Server on Redhat 7 Linux

    Forward from: https://linuxconfig.org/how-to-setup-vsftpd-ftp-file-server-on-redhat-7-linux How to s ...

  5. Python Day8

    Socket Socket是网络编程的一个抽象概念.通常我们用一个Socket表示"打开了一个网络链接",而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型 ...

  6. Java框架Struts2

    struts2的核心和工作原理   在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样的好处? 设计目标 Struts设计的第一目标就是使MVC模式应用于we ...

  7. 概率论与数理统计图解.tex

    \documentclass[UTF8,a1paper,landscape]{ctexart} \usepackage{tikz} \usepackage{amsmath} \usepackage{a ...

  8. 解决COM组件80070005错误

    前段时间在维护公司以前的项目时遇到一个问题,客户需要添加一个word文档合并功能,按理说这功能比较好实现,只要调用Office自带的COM组件就搞定了,但实际上并非如此,在客户端部署上以后运行报错,提 ...

  9. GridView 动态添加绑定列和模板列

    动态添加绑定列很简单:例如: GridView1.DataSourceID = "SqlDataSource1"; BoundField bf1 = new BoundField( ...

  10. [NHibernate]O/R Mapping基础

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) 引言 对象和关系数据库 ...