这道题借助二分查找算法来查找目标值的index

然后向前和向后分别搜索起始边界

注意开始排除异常值优化速度

#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'author' class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
length = len(nums)
if len(nums) == 0 or nums[0] > target or nums[length - 1] < target:
return [-1, -1]
index = self.binary_search(nums, 0, length - 1, target)
if index == -1:
return [-1, -1]
else:
start = index
end = index
for i in range(index - 1, -1, -1):
if nums[i] == target:
start = i
else:
break
for i in range(index + 1, length):
if nums[i] == target:
end = i
else:
break
return [start, end] #二分查找算法
def binary_search(self, nums, start, end, targrt):
if start > end:
return -1
mid = start + (end - start)//2
if nums[mid] > targrt:
return self.binary_search(nums, start, mid - 1, targrt)
elif nums[mid] < targrt:
return self.binary_search(nums, mid + 1, end, targrt)
else:
return mid

另外的一种思路是寻找 target-1 和 target+1的所在位置的索引,这两个值可能不存在,那么需要相应的修改二分查找算法

Leetcode-34-Search for a Range-(Medium)的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  2. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

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

  4. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  5. leetcode 34 Search for a Range(二分法)

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  6. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  7. leetcode@ [34] Search for a Range (STL Binary Search)

    https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...

  8. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  9. [leetcode 34] search for a range

    1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  10. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

随机推荐

  1. dbcp的配置

    tomcat的 配置,进入conf->context.xml <Resource name="mysql"     auth="Container" ...

  2. MVC 5 的 EF6 Code First 入门 系列:排序、筛选和分页

    这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第三篇:排序.筛选 ...

  3. 基于Web的IIS管理工具

    Servant:基于Web的IIS管理工具   Servant for IIS是个管理IIS的简单.自动化的Web管理工具.安装Servant的过程很简单,只要双击批处理文件Install Serva ...

  4. 冒泡排序最佳情况的时间复杂度,为什么是O(n)

    冒泡排序最佳情况的时间复杂度,为什么是O(n) 我在许多书本上看到冒泡排序的最佳时间复杂度是O(n),即是在序列本来就是正序的情况下. 但我一直不明白这是怎么算出来的,因此通过阅读<算法导论-第 ...

  5. IP 首部检验和算法

    原创博文,转载请注明出处. 在学习TCP/IP 详解的过程中遇到了不止一次的关于检验和的概念,在吸取了他人理解的前提下,我决定用Wireshark 进行抓包分析. 首先我们得知道IP数据包格式 首先把 ...

  6. Github官方app的账户身份验证的实现

    作为github上被fork最多,被star最多的Android项目,实在是没有理由不让我去学习学习的.这篇文章是关于其账户身份验证模块的实现的.如果你对此有兴趣,请移步到我的blog,地址如下: 地 ...

  7. [原创*精华]一键发布ASP.NET Web安装程序,搞WebForm的童鞋看过来...

          重要更新:鉴于很多小伙伴们说看不到图,我这边换了几个浏览器看了下,都看得到的,估计是网速问题,请耐心等待,另外,为了更好的方便大家学习,特此提供源码以及一个word文档,word文档就是本 ...

  8. json时间格式的互换

    c#代码 public class DateTimeUtil { /// <summary> /// 把json的时间格式还原-服务端 /// </summary> /// & ...

  9. MingQQ v1.0高仿版开源了,使用WebQQ协议实现了QQ客户端基本的聊天功能...

    MingQQ v1.0高仿版开源了,使用WebQQ协议实现了QQ客户端基本的聊天功能... MingQQ目前支持的功能如下:1.支持普通方式登录.验证码方式登录.注销.保持在线.改变在线状态.2.支持 ...

  10. 10169 - Urn-ball Probabilities !

    描述:有两个罐子,一个罐子里有一个红球,另一个罐子里有一个红球和一个白球,每次从两个罐子里各取一个球,然后在向每个罐子里各加一个白球,一次取到两个红球时就结束取球,求n次取球中至少一次为取球为红球的概 ...