[LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
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的更多相关文章
- (二分查找 拓展) 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 ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- [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 ...
- [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 ...
- 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 ...
- 刷题34. Find First and Last Position of Element in Sorted Array
一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...
- 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array
思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...
- 34. Find First and Last Position of Element in Sorted Array + 二分
题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...
随机推荐
- thinkphp中setInc、setDec方法
可用于统计字段(通常是数字类型的字段)的更新,例如积分,等级,登陆次数等 必须配合连贯操作where一起使用 score 是数据库指定的某个字段 $User = M("User" ...
- Chronic sleep loss cannot be cured that easily
Chronic sleep loss cannot be cured that easily Sleeping in on Saturday after a few weeks of too litt ...
- 二分图最大匹配|UOJ#78|匈牙利算法|边表|Elena
#78. 二分图最大匹配 从前一个和谐的班级,有 nlnl 个是男生,有 nrnr 个是女生.编号分别为 1,…,nl1,…,nl 和 1,…,nr1,…,nr. 有若干个这样的条件:第 vv 个男生 ...
- vue中的iviewUI导出1W条列表数据每次只导出2000条的逻辑
导出弹窗的html <template> <Modal v-model="exportModal" width=400 :closable="false ...
- 用Python下载美国国家气候数据中心(NCDC)的气候数据
美国国家气候数据中心的官网地址是https://www.ncdc.noaa.gov/ 气候数据的下载地址是: 长格式:ftp://ftp.ncdc.noaa.gov/pub/data/noaa/,这种 ...
- event.stopPropagation(),event.preventDefault()和return false的区别
event.stopPropagation(),event.preventDefault()和return false的区别 1.event.stopPropagation()方法 这是阻止事件的冒泡 ...
- 安装arcgis10.5不能启动服务的解决方案
最近由于公司需要,要装arcgis10.5,但是装这软件就费了好久的功夫.以前用的10.2,安装比较简单,但是10.5看起来就不一样了,下载完成后就会发现多了一个破解文件.按照教程一步一步安装的,但是 ...
- data-original
<img class="lazy" style="display: inline;" alt="开光纯铜牛摆件" src=" ...
- cocos2dx 常用的构建工具
理编辑工具Physics Editing ToolsMekanimo 网址:http://www.mekanimo.net/PhysicsBench 网址:http://www.cocos2d-iph ...
- mysql报错Establishing SSL connection without server's identity verification is not recommended
使用mysql数据库时报错:Establishing SSL connection without server's identity verification is not recommended ...