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. 自己的memcache类

    Mem类代码: class Mem {     //类型是memcache或memcached     private $type = 'Memcached';     //会话     privat ...

  2. h5 . css入门 2.CSS基础

    CSS基础 学习目标 1.CSS简介 2.CSS语法 3.样式的创建 4.两种引入外部样式表的区别 5.样式表的优先级和作用域 6.CSS选择器 7.选择器的权重 8.浮动属性的简单应用 9.HTML ...

  3. 壁虎书5 Support Vector Machine

    SVM is capable of performing linear or nonlinear classification,regression,and even outlier detectio ...

  4. oracle中如何生成awr报告

    oracle中如何生成awr报告   1.进入数据库 sqlplus / as sysdba 2.查看用户 show parameter db_name 3.开始压测后执行 exec DBMS_WOR ...

  5. CCPC-Wannafly Winter Camp Day4 Div1 - 咆咆咆哮 - [三分+贪心]

    题目链接:https://zhixincode.com/contest/18/problem/I?problem_id=267 题目描述 输入描述 输出描述 一行一个整数表示答案. 样例输入 1 32 ...

  6. [No0000114]远程桌面剪贴板无法同步本机,无法复制粘贴问题解决

    远程桌面无法与桌面共享复制内容(远程桌面复制之后,无法在本地桌面粘贴.反之亦然.),这时候需要杀掉一个进程并重新启动.[重启 rdpclip.exe] 1.在远程桌面中右键点击,选择启动任务管理器: ...

  7. 【绿书】 模拟,rep大坑

    https://vjudge.net/contest/229603#problem/B 绿书题 大模拟,绿书上用了个比较麻烦的输入,其实只要getchar()!='0'就行 坑: rep(i,0,s. ...

  8. CSS:概念和三种样式

    简介: CSS(Cascading Style Sheets):层叠样式表,它用来控制HTML标签的样式,给网页结构穿衣服~ CSS的编写格式是键值对的形式  ->  格式:属性名 : 属性值: ...

  9. iOS 抽取app中的图片图标资源

    iTunes 12.6之前的版本,我们手机连上MAC之后,可以在iTunes里看到应用选项,但是12.8之后的版本就不行了.无法通过iTunes 获取ipa文件进而获取APP图片资源. 不过还是有其他 ...

  10. prometheus: celery, redis-export

    https://github.com/nlighten/tomcat_exporter https://github.com/prometheus/jmx_exporter https://vexxh ...