# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 34: Search for a Range
https://oj.leetcode.com/problems/search-for-a-range/ 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]. ===Comments by Dabay===
二分查找。
当target在中间的时候,往两边扩展。
''' 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):
def expend(nums, index):
left = right = index
while left - 1 >= 0 and nums[left - 1] == nums[index]:
left -= 1
while right + 1 < len(nums) and nums[right + 1] == nums[index]:
right += 1
return [left, right] l, r = 0, len(A) - 1
while l <= r:
m = (l + r) /2
if A[m] == target:
return expend(A, m)
elif A[m] < target:
l = m + 1
else:
r = m - 1
else:
return [-1, -1] def main():
sol = Solution()
nums = [2,2]
target = 2
print sol.searchRange(nums, target) if __name__ == '__main__':
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]34: Search for a Range的更多相关文章

  1. 【LeetCode】34. Search for a Range

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  2. 【一天一道LeetCode】#34. Search for a Range

    一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...

  3. LeetCode:34. Search for a Range(Medium)

    1. 原题链接 https://leetcode.com/problems/search-for-a-range/description/ 2. 题目要求 给定一个按升序排列的整型数组nums[ ]和 ...

  4. LeetCode OJ 34. Search for a Range

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

  5. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  6. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  7. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  8. leetcode 34 Search for a Range(二分法)

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  9. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

随机推荐

  1. Python成长之路第一篇(4)_if,for,while条件语句

    有了以上的基本基础,已经上面写的几个小练习,大家肯定有很多的不满,比如查询为什么查询一次就退出了呢?下面我们来学习条件语句 一.万恶的加号 以前我们在print的时候如果要加上变量都有是使用+来作为连 ...

  2. MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在

    问题描述:在用vs生成MVC时若使用Internet应用程序为模版,项目建好后重新编译下无法通过,弹出错误: 解决方案:问题出来后,询问了身边很多人都是一头雾水,于是乎各种谷歌和百度,还好功夫不负有心 ...

  3. CC++初学者编程教程(6) 配置WindowsXP虚拟机与VC6.0

    1.我们安装需要下列文件. 2.新建一个虚拟机. 3. 选择默认的 Vmware Work Station10.0 4. 我们选择这个WindowsXP镜像. 5. 我们复制一个WindowsXPSP ...

  4. 调magento自定义模板发邮件

    1. 设置邮件模板 <global> <template> <email> <custom_email_template1 module="Samp ...

  5. php将文件夹打包成zip文件

    function addFileToZip($path,$zip){    $handler=opendir($path); //打开当前文件夹由$path指定.    while(($filenam ...

  6. OC中ARC forbids explicit message send of release错误(转)

    ARC forbids explicit message send of'release' 很显然,是ARC的问题. 错误原因:在创建工程的时候点选了“Use Automatic Reference ...

  7. 转发:为什么Android使用弱加密算法?

      Android 2.2.1默认使用的加密算法是AES 256-SHA1,但从2010年发布的Android 2.3开始,它默认使用的是一种更弱的加密算法 RC4-MD5. 当Android应用建立 ...

  8. 采用OSChina代码托管管理项目(一)

    .Git是什么 Git在Wikipedia上的定义:它是一个免费的.分布式的版本号控制工具,或是一个强调了速度快的源码管理工具. Git最初被Linus Torvalds开发出来用于管理Linux内核 ...

  9. wamp 虚拟目录的设置(转载)

    现在先来配置虚拟主机:1.先打开apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#!!2.打开apache的apach ...

  10. nade.js(一)进程管理

    简介 process是一个全局内置对象,可以在代码中的任何位置访问此对象,这个对象代表我们的node.js代码宿主的操作系统进程对象. 使用process对象可以截获进程的异常.退出等事件,也可以获取 ...