题目来源


https://leetcode.com/problems/search-for-a-range/

Given a sorted array of integers, find the starting and ending position of a given target value.

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].


题意分析


Input: a list and a target(int)

Output: a list with the first index and the last index of target in the input list

Conditions:时间复杂度为0(logn),两个index分别为起始和最后位置


题目思路

本题可采用二分查找的算法,复杂度是0(logn),首先通过二分查找找到taregt出现的某一个位置,然后以这个位置,以及此时的(first,last)来二分查找最左出现的位置和最右出现的位置

PS:注意边界条件


AC代码(Python)


 _author_ = "YE"
# -*- coding:utf-8 -*-
class Solution(object):
def findRight(self, nums, first, mid):
if nums[first] == nums[mid]:
return mid
nmid = (first + mid) // 2
if nmid == first:
return first
if nums[nmid] == nums[first]:
return self.findRight(nums, nmid, mid)
else:
return self.findRight(nums,first, nmid) def findLeft(self, nums, mid, last):
if nums[mid] == nums[last]:
return mid
nmid = (mid + last) // 2
if nmid == mid:
return last
if nums[nmid] == nums[last]:
return self.findLeft(nums, mid, nmid)
else:
return self.findLeft(nums,nmid + 1, last) def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
last = len(nums)
first = 0
while first < last:
mid = (first + last) // 2
# print(first,mid,last)
if nums[mid] == target:
# print(self.findLeft(nums,first, mid))
# print(self.findRight(nums,mid,last - 1))
return [self.findLeft(nums,first, mid), self.findRight(nums,mid,last - 1)]
elif nums[mid] < target:
first = mid + 1
else:
last = mid return [-1,-1]

[LeetCode]题解(python):034-Search for a Range的更多相关文章

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

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

  2. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  3. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  4. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  5. Java for LeetCode 034 Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  6. leetcode第33题--Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  7. [LeetCode 题解]: Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. 034 Search for a Range 搜索范围

    给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果在数组中找不到目标,返回 [-1, -1].例如:给出 [5, 7, 7, 8 ...

  9. 【LeetCode OJ 34】Search for a Range

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

  10. LeetCode(34)Search for a Range

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

随机推荐

  1. BZOJ1769 : [Ceoi2009]tri

    将所有点极角排序,建立线段树,线段树每个节点维护该区间内所有点组成的上下凸壳. 对于一个查询,二分查找出相应区间的左右端点,在线段树上得到$O(\log n)$个节点,在相应凸壳上三分查找出与斜边叉积 ...

  2. xamarin studio And linq 查询方式分析

    在 Windows 操作系统可以正常读取网络上的 https 数据流,在 Linux 操作系统中会失败:http://www.cnblogs.com/skyivben/archive/2012/03/ ...

  3. javascript第二弹——函数

    什么是函数 函数是一块javascript代码,被定义一次,就可以执行和调用多次:函数也是js对象,所以也可以像对象那样操作和传递:所以我们也把函数称之为函数对象: 创建函数的几种方法 一.函数声明 ...

  4. 解决EasyUI-Datagrid和LinqToEntity结合应用时排序问题

    我们在做WEB页面时,时常会选择JQuery框架的Datagrid,如Ext.EasyUI.Flexigrid,数据访问则采用LinqToSQL或LinqToEntity.UI用Jquery框架的目的 ...

  5. Shell 小技巧的问题 mysql -e ,字符串替换telnet命令检测

    api_url= #echo ${api_url/:/ }a #echo `echo $api_url|sed -i 's/:/ /' ` #|sed -i 's/:/ /' ` #sed 'as/: ...

  6. 外部调用JS文件时出现中文乱码的解决办法

    若测试网页的编码格式为:gb2312,而调用外部JS文件时出现了乱码(前提是JS文件无错误),则将调用的外部JS文件用记事本打开,然后再保存成编码格式为UTF-8的JS文件即可. 若测试网页的编码格式 ...

  7. Web移动端设计——移动设备分辨率一览表

    作为在移动端开发的web程序员来说,如果不懂设备的一些性能,在开发上面是非常耗时间的一件事,同时带来负面影响的是项目的进度被拖腿了. 下面是个人收集的一些移动端设备的分辨率参数: 1.  平板设备: ...

  8. ITIL图示

  9. 将request.getParameterMap()转换成可操作的普通Map

    在java web项目中虽然可以通过request.getParameterMap()很轻松的获得参数Map,但得到的Map和普通Map是不一样的,是被锁定的,不能像操作常规Map那样进行put.ge ...

  10. [学点英语]扎克伯格给女儿的信,translation of zucherber's letter to her daughter( Chinese version)

    A letter to our daughter 扎克伯格写给女儿的信   Mark Zuckerberg·Tuesday, December 1, 2015 Dear Max, 亲爱的玛克斯 You ...