题目来源


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. quick cocos 暂停场景

    local MainScene = class("MainScene", function() return display.newScene("MainScene&qu ...

  2. 转:ie6与firefox操作iframe中DOM节点的一点不同

    依次在两个浏览器中运行以下代码 <html> <body> <iframe id="myiframe"></iframe> < ...

  3. 【BZOJ】3224: Tyvj 1728 普通平衡树(某不科学的oj)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3224 无力吐槽,无力吐槽,无力吐槽....... bzoj竟然不能用time(0)我竟然不造!!re ...

  4. mysql中INSTR函数的用法

    mysql中INSTR函数的用法 INSTR(字段名, 字符串) 这个函数返回字符串在某一个字段的内容中的位置, 没有找到字符串返回0,否则返回位置(从1开始) SELECT * FROM tblTo ...

  5. WPF 中Frame + Page 的使用

    1 在window 的设计的时候 ,中间需要进行页面切换的时候,顶一个Frame <Frame Name="MainPage"  NavigationUIVisibility ...

  6. [转]超详细图解:自己架设NuGet服务器

    本文转自:http://diaosbook.com/Post/2012/12/15/setup-private-nuget-server 超详细图解:自己架设NuGet服务器 汪宇杰          ...

  7. 通过SEP屏蔽共享文件夹

    Go to Policies – Application and Device Control. Select default Application and Device Control polic ...

  8. java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim [问题点数:40分,结帖人wangxiaohua_001]

    14:56:10.093 WARN!! Error for /butterfly/plugins/zhonghang/UsefulData/save_usefuldata.bshjava.lang.N ...

  9. ul li横向排列及圆点处理

    如何用CSS制作横向菜单 让ul li横向排列及圆点处理   第一步:建立一个无序列表 我们先建立一个无序列表,来建立菜单的结构.代码是:<ul> <li><a href ...

  10. Nginx 笔记与总结(16)nginx 负载均衡

    nginx 反向代理时,如果后端有多台服务器,就可以实现负载均衡. 实现原理:把多台服务器用 upstream 绑定在一起并起一个组名,然后 proxy_pass 指向该组. ngx_http_ups ...