【LeetCode每天一题】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 a given targetvalue.Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4]
Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1]
思路
这道题最简单的思路就是一个从头开始遍历查找找到指定元素终止,一个从尾部开始查找直到指定元素终止。然后返回两个下标即为结果。时间复杂度为O(n),空间复杂度为O(1)。
第二种思路是我们运用我之前写的二分查找那篇博客中第一个经典问题就是查找有序数组中指定元素第一个出现的位置,找到之后然后向后遍历找到最后一个出现的位置,然后返回结果。时间复杂度为O(log n), 空间复杂度为O(1)。
第一种思路图示

第二种思路图示

第二种思路实现代码
class Solution(object):
def searchRange(self, nums, target):
if len(nums) < 1:
return [-1, -1]
start, end = 0, len(nums)-1
mid = 0
while start <= end: # 二分查找
mid = start+((end- start)>>1) # 取中间值
if nums[mid] < target:
start = mid+1
elif nums[mid] > target:
end = mid-1
else: # 当找到target值时,我们在进行查找,找到第一个出现的位置
if mid == 0 or nums[mid-1] != target:
break
end = mid-1
if nums[mid] != target: # 没找到,直接返回结果
return [-1, -1]
tem = mid
while tem < len(nums)-1 and nums[tem+1] == nums[tem]: # 找到最后出现的下标
tem += 1
return [mid, tem] 返回下标
第一种思路实现代码
class Solution:
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
start, end = -1, -1
for i in range(len(nums)): # 从头开始遍历
if nums[i] == target: # 找到下标
start = i
break if start < 0: # 没找到直接返回结果
return [-1, -1] for i in reversed(range(len(nums))): #从尾部向前开始查找
if nums[i] == target:
end = i
break
return [start, end] # 返回结果
【LeetCode每天一题】Find First and Last Position of Element in Sorted Array(找到排序数组中指定元素的开始和结束下标)的更多相关文章
- [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真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array
思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...
- [LeetCode]26. Remove Duplicates from Sorted Array删除排序数组中的重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- Leetcode算法【34在排序数组中查找元素】
在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
随机推荐
- day_4_25 py
''' 递归: 如果一个函数在内部不调用其它的函数, 而是自己本身的话,这个函数就是递归函数 ''' def factor(num): if num >1: result = num*facto ...
- MFC 应用程序中使用管道代码示意
STARTUPINFO sinf = {0}; PROCESS_INFORMATION pinf = {0}; SECURITY_ATTRIBUTES sa = {0}; HANDLE hPipeOR ...
- 快速构建springmvc+spring+swagger2环境
快速构建springmvc+spring+swagger2环境 开发工具:Intellij idea jdk: 1.8 开发步骤: 1.创建maven工程,如图建立工程结构 ...
- 微信生成二维码 只需一个网址即刻 还有jquery生成二维码
<div class="orderDetails-info"> <img src="http://qr.topscan.com/api.php?text ...
- linux下安装svn出现configure: error: We require OpenSSL; try --with-openssl
linux下安装svn出现configure: error: We require OpenSSL; try --with-openssl http://blog.csdn.net/woshixion ...
- ubuntu 安装NVIDIA驱动过程
用U盘引导安装时,提示 nouveau 0000:65:00.0: fifo: SCHED_ERROR 08 [] 解决方法: 进入grub 内核启动参数后增加 nomodeset nomodeset ...
- es6学习笔记入门总结
1.let const block 作用域 let 代替var 来声明块级作用域,没有变量提升,只在块内有作用 const 可以声明一个常量,类似于指针,指向某一个引用,这个常量并非一成不变的,但是不 ...
- base64简单使用
加密: import base64 import random str ="aqwertyuiopasdfghjklzxcvbnm963.0852741,.;'" a = '人生苦 ...
- eclipse几种常见问题的解决
build项目时出现卡死现象的解决方案 场景:在使用使用Eclipse编辑文件保存时或者build项目时,经常出现卡死现象,此时即便杀死eclipse进程重启还是依然出现这种现象. 原因:eclips ...
- tensorflow入门笔记(三) tf.GraphKeys
tf.GraphKeys类存放了图集用到的标准名称. 该标准库使用各种已知的名称收集和检索图中相关的值.例如,tf.Optimizer子类在没有明确指定待优化变量的情况下默认优化被收集到tf.Grap ...