[Leetcode][Python]34: Search for a Range
- # -*- 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的更多相关文章
- 【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 ...
- 【一天一道LeetCode】#34. Search for a Range
一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...
- LeetCode:34. Search for a Range(Medium)
1. 原题链接 https://leetcode.com/problems/search-for-a-range/description/ 2. 题目要求 给定一个按升序排列的整型数组nums[ ]和 ...
- 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 ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
随机推荐
- leetcode 算法刷题(一)
今天开始刷Leetcode上面的算法题.我会更新我刷题过程中提交的代码(成功和不成功的都有)和比较好的解法 第二题 Add Two Numbers 题目的意思:输入两个链表,这两个链表都是倒序的数字, ...
- MySQL----alter table modify | change的不同
1.modify.change都可以修改列的属性:一同的是modify只能修改表的数据类型.change比它要牛逼一点它可以在修改数据类型的同时也修改列名. 2.modify 的语法:alter ta ...
- Flex中如何通过horizontalTickAligned和verticalTickAligned样式指定线图LineChart横竖方向轴心标记的例子
原文http://blog.minidx.com/2008/12/03/1669.html 接下来的例子演示了Flex中如何通过horizontalTickAligned和verticalTickAl ...
- jQuery粘性跟随滚动条滚动的导航栏源代码下载
jQuery粘性跟随滚动条滚动的导航栏源代码下载 作者:网页模板 大小:0.005MB 点击次数:3494 发布时间:2014-03-07 12:55 分享到:0 特效介绍 jQuery粘性跟随滚动条 ...
- JavaScript压缩混淆 / 格式化 / 美化工具 - aTool在线工具
原文地址:http://www.atool.org/jscompression.php
- AndroidUI 视图动画-缩放动画效果 (ScaleAnimation)
放动画效果,可以使用ScaleAnimation: <Button android:id="@+id/btnScale2" android:layout_width=&quo ...
- 使用Vitamio打造自己的Android万能播放器(3)——本地播放(主界面、播放列表)
前言 打造一款完整可用的Android播放器有许多功能和细节需要完成,也涉及到各种丰富的知识和内容,本章将结合Fragment.ViewPager来搭建播放器的主界面,并实现本地播放基本功能.系列文章 ...
- UpdatePanel局部刷新用法
AjaxTest.aspx代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&qu ...
- 共享器 TS ERROR WINDOWS-FAILED 错误解决方法
问题:TS ERROR WINDOWS-FAILED 原因:微软操作系统自动更新补丁(KB956572)与终端机软件有冲突. 解决方法: .打开“开始菜单”: .打开“控制面板”: .打开“添加/删 ...
- [Android]Plug-in com.android.ide.eclipse.adt was unable to load class com.android.ide
今天启动eclipse的时候报了上述错误,打开xml是都报错.其实解决方法很简单:重启eclipse即可.