问题:给定一个有序数组和一个目标值,输出目标值在数组中的起始位置和终止位置,如果目标值不在数组中,则输出[-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. jxl java工具类,导出excel,导入数据库

    1: 引入jxl jar 我使用的为maven管理, <!--Excel工具--> <dependency> <groupId>net.sourceforge.je ...

  2. nvl()与regexp_replace()

    NVL(字段,0):将空值转化为0 regexp_replace(字段, ‘[1-9]‘ ,'') is not null; 将数字转化为0

  3. TCP黏包问题

    什么是黏包?什么情况下会出现黏包的情况?该如何避免黏包的情况? 首先来看一个例子 #服务端 import time from socket import * server = socket(AF_IN ...

  4. CountDownLatch,CyclicBarrier,Semaphore的使用

    什么时候使用CountDownLatch CountDownLatch原理和示例 Semaphore信号量的原理和示例 CyclicBarrier的用法 CyclicBarrier 和 CountDo ...

  5. Java for LeetCode 086

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  6. CodeForces - 799B T-shirt buying 【贪心】

    题目链接 http://codeforces.com/problemset/problem/799/B 题意 给出N件衣服 pi 表示 第i件衣服的价格 ai 表示 第i件衣服的前面的颜色 bi 表示 ...

  7. 《机器学习实战》学习笔记第三章 —— 决策树之ID3、C4.5算法

    主要内容: 一.决策树模型 二.信息与熵 三.信息增益与ID3算法 四.信息增益比与C4.5算法 五.决策树的剪枝 一.决策树模型 1.所谓决策树,就是根据实例的特征对实例进行划分的树形结构.其中有两 ...

  8. 分享知识-快乐自己:java 中的访问修饰符

    1):Java中的访问修饰符: Java面向对象的基本思想之一是封装细节并且公开接口.Java语言采用访问控制修饰符来控制类及类的方法和变量的访问权限,从而向使用者暴露接口,但隐藏实现细节. 访问控制 ...

  9. T61

    你参加了这次科学讨论会,有什么体会?What have you learned from the symposium?那墙有点斜.The wall is a little out of the per ...

  10. linux 命令:crontab

    一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...