[LeetCode]题解(python):034-Search for a Range
题目来源
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的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- LeetCode 034 Search for a Range
题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- 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 ...
- leetcode第33题--Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [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 ...
- 034 Search for a Range 搜索范围
给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果在数组中找不到目标,返回 [-1, -1].例如:给出 [5, 7, 7, 8 ...
- 【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- BZOJ3711 : [PA2014]Druzyny
设f[i]为[1,i]分组的最优解,则 f[i]=max(f[j]+1),max(c[j+1],c[j+2],...,c[i-1],c[i])<=i-j<=min(d[j+1],d[j+2 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- jBPM4.3+ssh+会签 整合配置及完整实例
大佬们的项目里有用到会签,所以趁双休日研究了下. 其实也是简单的会签情况,不过开始的时候研究了4.4,(因为先前研究的都是4.4),发现4.4跟4.3的处理方法完全不一样,搞的我比较郁闷……弄了一天, ...
- 【C语言】08-数组
为了让大家更好地学习和理解数组,我们先来认识一下内存中的"地址". 地址 1.计算机中的内存是以字节为单位的存储空间.内存的每一个字节都有一个唯一的编号,这个编号就称为地址.凡存放 ...
- [LintCode] Cosine Similarity 余弦公式
Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...
- Video Codecs by FOURCC 视频格式编码
FOURCC Name Summary 1978 A.M.Paredes predictor This is a LossLess video codec. >>> 2VUY 2VU ...
- 使用jenkins构建持续集成平台
jenkins + Maven + svn/git + tomcat 的持续集成平台 项目管理流程: 需求分析----原型设计----开发代码----提交测试-----内部测试-----确认上线( ...
- php 类
<?php class mysql{ private $host; private $name; private $pass; private $table; private $ut; func ...
- empty与isset的一点使用体会
刚在做表单提交的时候,我想检验一下数据是否存在,并用var_dump函数看一下数据.首先看使用isset()的代码 //登录函数 function login(){ if(!isset($_POST) ...
- java 文件读写
http://blog.csdn.net/jiangxinyu/article/details/7885518