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. 《JAVA编程思想》第四版 PDF 下载 中文版和英文版 高清PDF扫描带书签

    一.链接: 中文版: https://pan.baidu.com/s/1d07Kp4 密码:x2cd 英文版: https://pan.baidu.com/s/1boOSdAZ 密码: rwgm 文件 ...

  2. Django----Admin流程

    Admin执行步骤 启动文件: 1:创建app-----stark 2:在每个app中创建stark 3:django----admin---- 4:在stark中写入:--------------- ...

  3. atof()函数详解

    atof()函数 atof():double atof(const char *str ); 功 能: 把字符串转换成浮点数 str:要转换的字符串. 返回值:每个函数返回 double 值,此值由将 ...

  4. c# 编程小技巧

    1.对于界面布局,可以考虑使用 wpf,对于传统winfrom来说,tableLayoutPanel1可能是最好的选择. 2.你一定会问,如何使用代码管理大量的按钮,可以使用 List<Butt ...

  5. Codeforces 670F - Restore a Number - [字符串]

    题目链接:https://codeforces.com/contest/670/problem/F 题意: 有一个非负整数 $n$,在它的右侧添上它的位数后,被发送出去:例如 $6510$,加上位数 ...

  6. 【绿书】 模拟,rep大坑

    https://vjudge.net/contest/229603#problem/B 绿书题 大模拟,绿书上用了个比较麻烦的输入,其实只要getchar()!='0'就行 坑: rep(i,0,s. ...

  7. vue中导入外面文件(css,js)方式

    有时我们需要导入外面的css文件(例如reset.css文件,bootstrap.css,jQuery.js文件),通常可通过import "name.css"的形式 对于rese ...

  8. Converting Python Virtual Machine Code to C

    Converting Python Virtual Machine Code to C

  9. a buzzword to refer to modern Web technologies

    https://html.spec.whatwg.org/multipage/introduction.html#is-this-html5? HTML Living Standard — Last ...

  10. [模式匹配] AC 自动机 模式匹配

    广义的模式匹配: https://en.wikipedia.org/wiki/Pattern_matching 字符串模式匹配: https://en.wikipedia.org/wiki/Strin ...