# -*- 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. 网页设计之PS画渐变线条

    第一种线条的画法:画两条直线,这两条直线是 以背景色为基础 , 一个比背景色深  ,一个比背景色浅``. 第二种线条的画法:第一种画法是 图层样式 渐变叠加   叠加一个  背景色 到中间色 再到背景 ...

  2. yii2 安装过程中的问题及解决方法

    一.php版本要求5.4+,如果使用wamp组合包,建议更换 二.各种模块的支持,一般只要修改php.ini文件,去掉相应模块前的注释即可. 注意,Intl extension模块的支持需要将     ...

  3. codevs 1913 数字梯形问题 费用流

    题目链接 给你一个数字梯形, 最上面一层m个数字, 然后m+1,......m+n-1个. n是层数. 在每个位置, 可以向左下或右下走.然后让你从最顶端的m个数字开始, 走出m条路径, 使得路过的数 ...

  4. vs2005 测试 lua环境

    (1)添加文件核路径 (2)库文件路径 (3)main.cpp #include <stdio.h>#include <string.h> extern "C&quo ...

  5. Jquery remove()和empty()

    要用到移除指定元素的时候,发现empty()与remove([expr])都可以用来实现.可仔细观察效果的话就可以发现.empty()是只移除了 指定元素中的所有子节点,拿$("p" ...

  6. 论山寨手机与Android 【14】3G SmartPhone时代的MTK

    分析了SmartPhone的里里外外以后,现在我们可以分析MTK的机遇和挑战了.MTK面临的外部环境在发生变化,变化有两条,一是移动网络从2G演变到3G,二是手机由FeaturePhone演化到Sma ...

  7. 2014第11周四Eclipse开发问题记

    今天开发中eclipse工具使用上又学到几点: 1.去除代码空行:在Find输入框中输入:^\s*\n然后替换为空即可: 2.eclipse插件的加载:对于单一个jar文件的插件,直接放在plugin ...

  8. ssh 应用

    SSH反向连接及Autossh ssh 隧道: http://www.cnblogs.com/robinyjj/archive/2008/11/02/1325018.html This guy wri ...

  9. linux学习之(三)-文件操作命令

    创建一个空文件: touch  文件名 例:touch   tom 查看: 查看一个文件的内容命令cat 文件名 例:cat tom   注:cat命令并不能显示文件的所有信息,但屏幕显示的 行数是有 ...

  10. python升级导致的坑

    问题来源 问题往往都是这样来的突然,让我措手不及. 小孩没娘说来话长啊,操作系统是centos6.5因此默认自带的python是2.6.6的,突然有一天我要写一个关于kafka topic消费情况的监 ...