Description

Given a sorted array of n integers, find the starting and ending position of a given target value.

If the target is not found in the array, return [-1, -1].

Example

Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

Challenge

O(log n) time.

这个题目的思路就是用两个binary search, 分别求first index 和last index.

Code   其实可以利用helper funciton来去优化code.

class Solution:
def searchRange(self, A, target):
# write your code here
if not A: return [-1,-1]
l, r , ans = 0, len(A) -1, [-1,-1]
while l + 1 < r:
mid = l + (r - l)//2
if A[mid] < target:
l = mid
elif A[mid] > target:
r = mid
else:
r = mid
if A[l] == target:
ans[0] = l
elif A[r] == target:
ans[0] = r
else:
return ans # find last index
l, r = 0, len(A) -1
while l + 1 < r:
mid = l + (r - l)//2
if A[mid] < target:
l = mid
elif A[mid] > target:
r = mid
else:
l = mid
if A[r] == target:
ans[1] = r
elif A[l] == target:
ans[1] = l
else:
return ans
return ans

去掉不必要的行

class Solution:
def searchRange(self, A, target):
# write your code here
if not A: return [-1,-1]
l, r , ans = 0, len(A) -1, [-1,-1]
while l + 1 < r:
mid = l + (r - l)//2
if A[mid] < target:
l = mid
else:
r = mid
if A[l] == target: ans[0] = l
elif A[r] == target: ans[0] = r
else:
return ans # find last index
l, r = 0, len(A) -1
while l + 1 < r:
mid = l + (r - l)//2
if A[mid] <= target:
l = mid
else:
r = mid
if A[r] == target: ans[1] = r
elif A[l] == target: ans[1] = l
return ans

Use helper function to make the code even shorter.

class Solution:
def searchRange(self, A, target):
l, r = 0, len(A) - 1
if not A or nums[l] > target or nums[r] < target:
return [-1, -1]
def helper(points, pos, target):
while points[0] + 1 < points[1]:
mid = points[0] + (points[1] - points[0])//2
if A[mid] > target:
points[1] = mid
elif A[mid] < target:
points[0] = mid
else:
points[pos] = mid
if A[points[1 - pos]] == target: return points[1 - pos]
if A[points[pos]] == target: return points[pos]
return -1
return [helper([l, r], 1, target), helper([l, r], 0, target)]

[LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search的更多相关文章

  1. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  2. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  3. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  4. [leetcode]34.Find First and Last Position of Element in Sorted Array找区间

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  5. leetcode [34] Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  6. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  7. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  8. leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array

    思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...

  9. 34. Find First and Last Position of Element in Sorted Array + 二分

    题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...

随机推荐

  1. day_4.28 py

    2018-4-28 15:13:39 ''' 在方法名字加入两个 __则为私有方法 类似于private 方法 ''' class Dog: #私有方法 def __send_msg(self): p ...

  2. Ubuntu下搭建高匿HTTP代理(亲测可用)

    功能用途 我们在生活中见过各种代理,比如我们距离火车站较远,我们可以选择通过距离最近的火车票代售点来购买火车票.又比如商品代理商,我们拿不到厂家的直接或者,可以通过厂家授权的代理经销商来获得产品.代理 ...

  3. ARM Linux Oops使用小结(转)

    出现Oops消息的大部分错误时因为对NULL指针取值或者因为用了其他不正确的指针值. Oops如何产生的解释如下:     由于处理器使用的地址几乎都是虚拟地址,这些地址通过一个被称为“页表”的结构被 ...

  4. Docker多主机网络

    网络术语概念 二层交换技术:工作在OSI七层网络模型的第二层,通过MAC地址进行帧转发 三层交换技术:也称为IP交换技术,工作在OSI七层网络模型的第三层,通过IP地址进行包转发.它解决了局域网中网段 ...

  5. np.mgird np.ogrid

    np.ogrid: address:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ogrid.html returns an o ...

  6. DB2 rollforward 命令使用详解

    DB2 rollforward 命令使用详解 原文:https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1003wuc ...

  7. PHP微信支付开发

    此链接https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_2,是微信官方的示例,无效,报错. 1.申请微信支付的开通条件?什么样的账号可以 ...

  8. 一个DRAM的存储单元存储的是0还是1取决于电容是否有电荷,有电荷代表1,无电荷代表0。

    小结: 1.一个DRAM的存储单元存储的是0还是1取决于电容是否有电荷,有电荷代表1,无电荷代表0. https://baike.baidu.com/item/随机存取存储器/4099402?fr=a ...

  9. iOS-原生纯代码约束总结(一)之 AutoresizingMask

    一,概述 iOS有两大自动布局利器:autoresizing 和 autolayout(autolayout是IOS6以后新增).autoresizing是UIView的属性,一直存在,使用也比较简单 ...

  10. [math][mathematica] mathematica入门

    快速入门手册: 只找到了个中文的快速入门: https://www.wolfram.com/language/fast-introduction-for-programmers/zh/?source= ...