# -*- 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. Android 测试工具集01

    Appium是一个支持原生,混合和移动web apps的开源的跨平台测试框架工具. ANDROID依赖 Android SDK API >= 17 (Additional features re ...

  2. 关于Eclipse的编码配置和字体大小设置

    编码设置 1.工作空间编码:Window-->Preferences-->General-->Workspace 2.工程文件编码:项目-->Properties-->R ...

  3. PhpStorm 4.0 & 5.0 部署本地Web应用

    1.创建新的项目(project),创建完成之后单击工具栏的应用运行/调试(Select Run/Debug Configuration)的下拉菜单弹出 Edit Cofigurations选项,单击 ...

  4. Linux 计算某文件夹下的所有文件的md5值

    使用find 命令 find /root -type f -print0 |xargs -0 md5sum >a.md5 校验的话 md5sum -c a.md5

  5. [转]数组引用:C++ 数组做参数 深入分析

    "数组引用"以避免"数组降阶"(本文曾贴于VCKBASE\C++论坛) 受[hpho]的一段模板函数的启发,特写此文,如有雷同,实在遗憾. 数组降阶是个讨厌的事 ...

  6. 写给初学者css优先级问题

    首先需要搞清楚几个基本概念 1.内嵌样式: 写在元素标签内的例如:<div style="background-color:red"> </div> 2.内 ...

  7. MVC 创建带图片的<A></A>标签

    <a href="@Url.Action("Detail", "Product", new { messageId = item.message ...

  8. 使用Idea编写javaweb以及maven

    使用Idea编写javaweb以及maven 今天总结的第一点是在windows下使用idea编写jsp并且使用tomcat部署:第二点是新建maven项目,之前一直是听说也没有自己实践过,今天就大概 ...

  9. Oracle EBS-SQL (WIP-6):检查任务已完成但状态是发放的任务.sql

    select WE.WIP_ENTITY_NAME         ,MSI.SEGMENT1         ,MSI.DESCRIPTION         ,WDJ.CLASS_CODE     ...

  10. 测试Web服务接口

    1. http://www.iteye.com/topic/142034 2. http://www.iteye.com/topic/1123835 3.http://yongguang423.ite ...