[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 ...
随机推荐
- 阿里云的linux命令小结
/** ---------------- [ 华丽分割线 ] ------------------------ ### 121.40.120.167 操作 ### 1.启动 nginx cd /usr ...
- vs2012 发布网站丢失文件
问题描述 在发布网站时,发现上线的网站总是功能缺失,而本地代码确实没问题. 到发布网站的磁盘去查看,发现丢失了很多静态页面文件. 这是一个很奇怪的问题:mvc的网站,丢失了很多View(大部分的csh ...
- css实现两端对齐的3种方法
两端对齐在移动端非常见,说到两端对齐,大家并不陌生,在word.powerpoint.outlook等界面导航处,其实都有一个两端对齐(分散对齐)的按钮,平时使用的也不多,我们更习惯与左对齐.居中对齐 ...
- I/O浅析
1.为什么需要I/O? 因为程序需要从别的文件中获取内容或者程序要将自身的内容传入到文件中. 2.流种类的概述 1.字节流和字符流 字节流的基础单位是byte 字符流 ...
- div模拟textarea以实现高度自适应实例页面
作为多行文本域功能来讲,textarea满足了我们大部分的需求.然而,textarea有一个不足就是不能像普通div标签一样高度可以跟随内容自适应.textarea总是很自信地显摆它的滚动条,高度固执 ...
- web 之MVC
MVC 把一个应用的输入.处理.输出流程按照Model, View, Controller 的方式进行分离,这样一个应用被分为三层:模型层.视图层.控制层. 1.View 2.Controller 在 ...
- delphi 实现微信开发
大体思路: 1.用户向服务号发消息,(这里可以是个菜单项,也可以是一个关键词,如:注册会员.) 2.kbmmw web server收到消息,生成一个图文消息给微信,在图文消息中做好自己的url,在u ...
- Android studio gradle配置!!!【转】
转自:http://www.open-open.com/lib/view/open1415793464648.html Gradle 基本概念 首先我们学习几个gradle 的脚本语法,掌握了这几个语 ...
- Javascript基本算法演练 Seek and Destroy
转载自:http://aeroj-blog.logdown.com/posts/435808 Seek and Destroy You will be provided with an initial ...
- 【插队问题-线段树-思维巧妙】【poj2828】Buy Tickets
可耻的看了题解 巧妙的思维 逆序插入,pos 代表的意义为前面要有pos个空格才OK: 证明:仔细思考一下就觉得是正确的,但是要想到这种方式还是要很聪明,空格是前面的几个数字所形成的,所以要特地留出来 ...