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. nodejs--get请求数据解析

    ---- 三种方式解析: 1.自动动手切 2.api的querystring模块 3.api的url模块

  2. mysql日期格式转换,如何保持原日期?CONVERT/Substring 函数截取。replace替换

    http://www.cnblogs.com/stevenjson/p/3729577.html CONVERT(varchar(100), getdate(), 112)这种, 问题就出在getda ...

  3. SPOJ - TSUM 母函数+FFT+容斥

    题意:n个数,任取三个加起来,问每个可能的结果的方案数. 题解:构造母函数ABC,比如现在有 1 2 3 三个数.则 其中B表示同一个数加两次,C表示用三次.然后考虑去重. A^3表示可重复地拿三个. ...

  4. A. Srdce and Triangle 几何题

    链接:https://www.nowcoder.com/acm/contest/104/A来源:牛客网 题目描述 Let  be a regualr triangle, and D is a poin ...

  5. weakSelf 运用 strongSelf来解决block的循环引用

    SDWebImage 中有一段源码: #if SD_UIKIT Class UIApplicationClass = NSClassFromString(@"UIApplication&qu ...

  6. i-chips融合芯片分析

    适合做图像变形和融合 http://www.i-chips.co.jp/products/ma_non-deployment/c786.html 下面视频其实用了2个芯片,其中IP00C733用原始信 ...

  7. [https][ssl] keyless SSL

    HTTP Server 集群前的负载设备,或内容审计设备等,在处理https的时候,需要用户配置提供证书. 但是考虑到安全问题,HTTP Server并不愿意把证书配置到其他设备上. 这个时候,就有个 ...

  8. Flink – metrics V1.2

    WebRuntimeMonitor   .GET("/jobs/:jobid/vertices/:vertexid/metrics", handler(new JobVertexM ...

  9. iOS将excel转plist

    iOS将excel转plist 先把excel用Numbers打开,转换成CSV,然后新建一个工程,写下面的代码: - (void)viewDidLoad { [super viewDidLoad]; ...

  10. eclipse中tomcat无法加载spring boot

    转自: http://blog.csdn.net/u010797575/article/details/50517777 最近搭建一套spring boot框架,作为 application 启动项目 ...