[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) 分 ...
随机推荐
- E - Coin Game
After hh has learned how to play Nim game, he begins to try another coin game which seems much easie ...
- 不同的GCD算法
分类: C语言程序2014-10-08 15:10 28人阅读 评论(0) 收藏 举报 gcdC语言程序位运算 早在公元前300年左右,欧几里得就在他的著作<几何原本>中给出了高效的解法- ...
- 源码安装git工具,显示/usr/local/lib64/libcrypto.a(dso_dlfcn.o) undefined reference to `dlopen'
/usr/local/lib64/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':dso_dlfcn.c:(.text+0x30) ...
- hdu4763 Theme Section【next数组应用】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- grafana----alert
Alert只有grafana V4.0以上. Introduction(介绍) Grafana中的alert允许在dashboard panels你附加一些规则.当你保存仪表板Grafana将提取的报 ...
- MyBatis中choose when正确写法
<choose> <when test="scoreRange!=null and scoreRange eq 1"> AND sc.score <! ...
- A Method for the Construction of Minimum-Redundancy Codes
A Method for the Construction of Minimum-Redundancy Codes http://compression.ru/download/articles/hu ...
- [qemu][cloud][centos][ovs][sdn] centos7安装高版本的qemu 以及 virtio/vhost/vhost-user咋回事
因为要搭建ovs-dpdk,所以需要vhost-user的qemu centos默认的qemu与qemu-kvm都不支持vhost-user,qemu最高版本是2.0.0, qemu-kvm最高版本是 ...
- Java中包装类型和基本类型的使用场景(什么时候使用包装类型)(转)
说明:最简单的理解,基本类型有默认值,而包装类型初始为null.然后再根据这两个特性进行分业务使用,在阿里巴巴的规范里所有的POJO类必须使用包装类型,而在本地变量推荐使用基本类型. Java语言提供 ...
- Mysql 数据库几种引擎的区别比较
· MyISAM:默认的MySQL插件式存储引擎,它是在Web.数据仓储和其他应用环境下最常使用的存储引擎之一.注意,通过更改STORAGE_ENGINE配置变量,能够方便地更改MySQL服务器的默认 ...