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(找到排序数组中指定元素的开始和结束下标)的更多相关文章

  1. [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 ...

  2. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  3. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)

    这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  4. leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array

    思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...

  5. [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 ...

  6. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  7. Find First and Last Position of Element in Sorted Array - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...

  8. Leetcode算法【34在排序数组中查找元素】

    在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...

  9. [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 ...

随机推荐

  1. mui---自定义页面打开的方向

    在使用MUI做APP的时候,会考虑对页面的打开方向做规定,MUI也给我们提供了很多种页面的打开方式. 具体参考: http://ask.dcloud.net.cn/question/174 MUI做A ...

  2. hashlib

    登录认证 加密 --> 解密 摘要算法 两个字符串 : import hashlib # 提供摘要算法的模块 md5 = hashlib.md5() md5.update(b') print(m ...

  3. Linux CentOS中防火墙的关闭及开启端口

    注:CentOS7之前用来管理防火墙的工具是iptable,7之后使用的是Firewall 样例:在CentOS7上安装tomcat后,在linux本机上可以访问tomcat主页,http://ip: ...

  4. 7:CSS Sprites的原理(图片整合技术)

    7:CSS Sprites的原理(图片整合技术) 一.将导航背景图片,按钮背景图片等有规则的合并成一张背景图,即将多张图片合为一张整图,然后用background-position”来实现背景图片的定 ...

  5. org.hibernate.HibernateException: connnection proxy not usable after transaction completion

    今天yuan男神的程序报了这个错, getHibernateTemplate().saveOrUpdate(obj); getHibernateTemplate().flush(); getHiber ...

  6. ASP.NET异步

    1.ASP.NET线程模型 在WEB程序中,天生就是多线程的,我们知道,一个WEB服务可以同时服务器多个用户,我们可以想象一下,WEB程序应该运行于多线程环境中,对于运行WEB程序的线程,我们可以称之 ...

  7. stm32中断遵循原则

    故障案例: 定时器定时触发一个定时事件,在这个事件里面,会调用一个串口发送程序,发现串口发送数据不完整. 分析: 1.将发送函数剥离,放到独立的线程工作,运行稳定 2.使用单步调试,在定时中断事件中多 ...

  8. Ubuntu下eclipse中运行Hadoop时所需要的JRE与JDK的搭配

    第一组: Eclise 版本:Indigo,Service Release 1 Build id:20110916-0149 Window-->Preferences -->Compile ...

  9. [No0000157].net core项目中拼音,excel,pdf处理库

    汉字转拼音 1. HxfPinYin public static class Pinyin { public static string ConvertEncoding(string text, En ...

  10. ARCSDE直连Oracle时出现错误Failed to connect to the specified server. Underlying DBMS error[ORA-12154: TNS:could not resolve the connect identifier specified. No extended error]

    买了新笔记本,装软件. 在ARCSDE直连Oracle时遇到问题. esri官网给的解释是因为安装arcgis时安装目录里有特殊字符(详见:https://support.esri.com/en/te ...