[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) 分 ...
随机推荐
- apache 二级域名设置完整步骤
步骤如下: 1. 你要拥有一个有泛域名解析的顶级域名,例如:abc.com 在dns服务上设置,域名服务商都提供此服务 www.abc.com 指向服务器IPabc.com ...
- Goroutines
Go 语言中的并发可以用两种方式实现: 第一种方式,支持顺序通信进程(communicating sequential processes),简称 CSP.CSP是一种现代的并发编程模型,在这种编程模 ...
- 修改Jenkins的主目录步骤
在使用Jenkins做持续集成过程中,在构建很多次后发现有时在构建的时候系统提示磁盘空间不足,此时检查发现Jenkins的主目录挂载区放在了服务器根目录下,占用空间较大,此时除了对服务器的磁盘进行扩容 ...
- js积累点
window.opener.parent.frames['taskAnswerInfoForm'].location=newUrl;//可以使该frame的页面跳转.不需要再写xxx.location ...
- svn异常:subversion.javahl.ClientException
使用svn时出现异常: INFO [org.netbeans.modules.subversion]: org.apache.subversion.javahl.ClientException: Pr ...
- 洛谷P1434 滑雪【记忆化搜索】
题目:https://www.luogu.org/problemnew/show/P1434 题意: 给一个矩阵,矩阵中的数字代表海拔高度. 现在要找一条最长路径,使得路径上的海拔是递减的. 思路: ...
- [No0000106]配置PLSQL,提升工作效率
界面模板的配置: 方便用户快速点击需要的功能.如打开SQL Window 1.打开customize,用户自定义Toolbars对话框. 2.在Commands命令标签页,选中要添加的命令,拖动到工具 ...
- python--列表、字典、元组、集合对比
数据类型# 计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数据类型.# a:整形和浮点型(正数和负数)# b:布尔类型(true,fals ...
- jQuery 报错,对象不支持tolowercase属性或方法
泪流满面.<input>里id和name都不能是nodeName,否则跟jquery.js冲突 JQuery 实践问题 - toLowerCase 错误 在应用JQuery+easyui开 ...
- set,env,export,set -x,set -e;
set 用来显示本地变量 env 用来显示环境变量 export 用来显示和设置环境变量 set 显示当前shell的变量,包括当前用户的变量 env 显示当前用户的变量 export 显示当前导出成 ...