问题:给定一个有序数组和一个目标值,输出目标值在数组中的起始位置和终止位置,如果目标值不在数组中,则输出[-1,-1]

示例:

输入:nums = [1,2,3,5,5,7] target = 5

输出:[3,4]

输入:nums = [1,5,8,9] target = 7

输出:[-1,-1]

解决思路:二分查找直到找到第一个目标值,再对目标值左右进行二分查找

Python代码:

class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
l = 0
r = len(nums)-1
while l <= r :
mid = (l+r)//2
if nums[mid] > target:
r = mid - 1
elif nums[mid] < target:
l = mid + 1
else:
first,last = mid,mid
while l <= first:
fm = (first+l) // 2
if nums[fm] < target:
l = fm + 1
else:
first = fm -1
while r >= last:
rm = (last+r) // 2
if nums[rm] > target:
r = rm - 1
else:
last = rm + 1
return [first+1,last-1]
return [-1,-1]

Find First and Last Position of Element in Sorted Array的更多相关文章

  1. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  2. leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array

    leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array Given an array of int ...

  3. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  4. Find First and Last Position of Element in Sorted Array - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...

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

  6. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  7. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  8. Leetcode: Find First and Last Position of Element in Sorted Array

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

  9. [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | Find First and Last Position of Element in Sorted Array

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

  10. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

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

随机推荐

  1. Dijkstra的堆优化

    先附上一个例题:P3371 [模板]单源最短路径 一眼扫去,最短路... spfa可行,但是今天的主题是Dijkstra: #include<iostream> #include<a ...

  2. java基础之容器、集合、集合常用方法

    一.容器(Collection):数组是一种容器,集合也是一种容器 java编程中,装其他各种各样的对象(引用类型)的一种东西,叫容器 注意: 1.数组的长度是固定的 2.集合:长度不固定, 可以随时 ...

  3. HDU - 2612 Find a way 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...

  4. 《C prime plus (第五版)》 ---第12章 存储类.链接和内存管理

    12-1:存储类: 1.作用域: 代码块作用域,函数原型作用域和文件作用域. 2.链接:分为外部链接,内部链接和空链接.代码块作用域和函数原型作用域都是空连接,意味着是私有的.而文件作用域的变量可能是 ...

  5. android 电池(一):锂电池基本原理篇【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/8497830 关键词:Android  电池关机充电 androidboot.mode ...

  6. Linux学习之路(二)文件处理命令之下

    分区格式化: 一块分区想要使用的话,要格式化.格式化主要有两个工作,1,把分区分成等大小的数据块,每个数据块一般为4KB.2在分区之前建一个分区表,给第一个文件建一行相关数据,在分区表里保存了它的io ...

  7. 分享知识-快乐自己:oracle12c创建用户提示ORA-65096:公用用户名或角色无效

    今天在oracle12c上创建用户,报错了.如下图: 我很郁闷, 就打开了oracle官方网站找了下, 发现创建用户是有限制的. 2.解决方案 创建用户的时候用户名以c##或者C##开头即可. 错误写 ...

  8. BZOJ-4327:JSOI2012 玄武密码(AC自动机模板题)

    在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此.  很多年后,人们终于在进香 ...

  9. MySQL上周新增激活用户在上周下单情况_20161107周一

    上周新增激活用户在上周下单情况 1.上周激活用户明细 #上周激活用户明细 SELECT a.城市,a.用户ID,a.用户名称,b.用户地址,b.联系电话,a.订单日期,c.年周,c.上周一,a.订单I ...

  10. bzoj 2251: 外星联络 后缀Trie

    题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=2251 题解 本来以为这道题应该从01序列的性质入手 结果就想歪了 等自己跳出了01序列这个 ...