这道题借助二分查找算法来查找目标值的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. 日期,为下拉列表添加日期,优化,目前本人博客上最优的解决方案,之前学习的通过判断得到平年闰年,而这个是让系统自动去判断,无须if判断,代码示例

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  2. VS2012下systemC配置

    一.编译System库 1.下载SystemC library source code               到http://www.systemc.org注册会员账号后,即可下载SystemC ...

  3. Asp.Net Identity 2.0 认证

    转Asp.Net Identity 2.0 认证 一个星期前,也就是3月20日,微软发布了Asp.Net Identity 2.0 RTM.功能更加强大,也更加稳定.Identity这个东西现在版本还 ...

  4. Confluence, JIRA, Fisheye

    [tools]迁移Confluence, JIRA, Fisheye   [背景] 原先的Confluence, JIRA, Fisheye都部署在一台服务器(192.168.200.203)上,导致 ...

  5. RPC(Remote Procedure Call Protocol)

    远程过程调用协议: 1.调用客户端句柄:执行传送参数 2.调用本地系统内核发送网络消息 3.消息传送到远程主机 4.服务器句柄得到消息并取得参数 5.执行远程过程 6.执行的过程将结果返回服务器句柄 ...

  6. 企业架构研究总结(38)——TOGAF架构能力框架之架构能力建设和架构治理

    为了确保架构功能在企业中能够被成功地运用,企业需要通过建立适当的组织结构.流程.角色.责任和技能来实现其自身的企业架构能力,而这也正是TOGAF的架构能力框架(Architecture Capabil ...

  7. MongoDB学习(翻译2)

    C#驱动之LINQ教程 介绍 本教程涵盖了1.8发布版本对linq查询的支持. 开始本教程之前,你应该至少阅读下C#驱动教程关于C#驱动的介绍 快速开始 首先,添加下面命名空间到您的程序中: usin ...

  8. IE8下提示'console'未定义错误

    在开发的过程中由于调试的原因,在代码中加入console.info("xxxx"),而未进行删除 在IE8下测试该代码所在的页面报错,如下: 需要注意的是,使用console对象查 ...

  9. 跟我一起玩转Sencha Touch 移动 WebApp 开发1

    跟我一起玩转Sencha Touch 移动 WebApp 开发(一) 1.目录 移动框架简介,为什么选择Sencha Touch? 环境搭建 创建项目框架,框架文件简介 创建简单Tabpanel案例 ...

  10. 各种排序方法的JS实现

    各种排序算法的对比总结如下表所示: 冒泡排序: 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完 ...