【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 ...
随机推荐
- DOM常用事件绑定方式与实例
一.常用的事件 onclick 点击事件 模态框实例 <input type="button" id="b1" style="width:50p ...
- windous----操作系统基础
操作系统基础 服务软件,控制硬件. 一:什么事操作系统 操作系统就是一个协调,管理和控制和计算机硬件资源控制程序. 用户态:运行应用程序,不可以操作硬件(可以获取cpu的指令集的一个子集,该子集不包 ...
- 使用介质设备安装 AIX 以通过 HMC 安装分区
使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...
- 恢复制作了系统盘的U盘
制作了系统盘的U盘通常容量会变得很小(比如用win32制作的系统盘) 此时在系统安装完成之后就要把U盘恢复,否则就无法正常使用了 步骤: 1.win+r打开程序搜索框,输入cmd打开dos窗口 2.在 ...
- CCPC-Wannafly Winter Camp Day4 Div1 - 咆咆咆哮 - [三分+贪心]
题目链接:https://zhixincode.com/contest/18/problem/I?problem_id=267 题目描述 输入描述 输出描述 一行一个整数表示答案. 样例输入 1 32 ...
- python3写入csv文件时中文为乱码
今天修改李万的爬虫时把页面上的中文写入csv文件时,中文总是乱码.通过上网搜索得到解决.解决的办法是打开文件是需加参数 encoding='utf-8-sig' .感谢博客园的菜鸟Alex.他相关博客 ...
- oracle查表技巧
查每张表中有多少条记录 SELECT table_name,num_rows FROM user_tables order by num_rows desc; https://www.cnblogs. ...
- 转:jdk动态代理实现
原文链接: jdk动态代理 注:文章中用常用的流程实现 动态代理,流程逻辑比较清晰.文章后面对 “为什么要使用接口” 原理分析还未细看. jdk的动态代理为什么用接口,内部是什么原理呢?看了几篇文章貌 ...
- C++生成静态库
//StaticMath.h #include <iostream> class StaticMath { public: //StaticMath(void); //~StaticMat ...
- Web Deploy远程部署配置图解
原文链接:https://jingyan.baidu.com/album/642c9d34e614de644a46f783.html