# -*- 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. js删除数组里的某个元素

    首先可以给js的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为: Array.prototype.indexOf = function(val) { for (var i = ...

  2. Android_CodeWiki_03

    1.发送不重复的通知(Notification) public static void sendNotification(Context context, String title, String m ...

  3. 分享个人如何DIY网站的经验

    对于一个接触过Web开发的IT人来说,一般都考虑过创建属于自己的网站,可能是定制自己特有风格的博客类网站,可能是私密的个人主页,也可能是展示自己开源工具的网站,当然,酝酿着做个商业网站来创业的人肯定也 ...

  4. 【git】error: Your local changes to the following files

    今天在服务器上git pull是出现以下错误: error: Your local changes to the following files would be overwritten by mer ...

  5. QueryString和BASE64

    加号(+)是BASE64编码的一部分,而加号在QueryString中被当成是空格.因此,当一个含有BASE64编码的字符串直接作为URL的一部分时,如果其中含有加号,则使用QueryString读取 ...

  6. Unity3d GUI弹窗

    ArrayList w_position = new ArrayList(); void OnGUI() { if (GUILayout.Button("Open")) { if ...

  7. ln 命令

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...

  8. 网易云课堂_程序设计入门-C语言_第二周:判断_1时间换算

    1 时间换算(5分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果 ...

  9. Zookeeper介绍

    Zookeeper是一个分布式的开源系统,目的是为分布式应用提供协调一致性服务. 分布式应用可以在Zookeeper提供的简单原语集之上构造更高层次的服务.比如统一命名服务.状态同步服务.集群管理.分 ...

  10. hadoop记录topk

    lk@lk-virtual-machine:~$ cd hadoop-1.0.1 lk@lk-virtual-machine:~/hadoop-1.0.1$ ./bin dfs -mkdir inpu ...