Find First and Last Position of Element in Sorted Array
问题:给定一个有序数组和一个目标值,输出目标值在数组中的起始位置和终止位置,如果目标值不在数组中,则输出[-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的更多相关文章
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 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 ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- [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 ...
- 刷题34. Find First and Last Position of Element in Sorted Array
一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...
- [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 ...
- 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 ...
- [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 ...
- (二分查找 拓展) 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 ...
随机推荐
- Django框架ORM常用字段汇总_模型层
与数据类型相关的字段 CharField 作用:字符串字段, 用于较短的字符串. 参数:CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许 ...
- PHP上传视频
https://github.com/chaping/plupload_docs 这下载 lupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统 ...
- POJ - 2299 Ultra-QuickSort 【树状数组+离散化】
题目链接 http://poj.org/problem?id=2299 题意 给出一个序列 求出 这个序列要排成有序序列 至少要经过多少次交换 思路 求逆序对的过程 但是因为数据范围比较大 到 999 ...
- ZOJ - 3861 Valid Pattern Lock 【全排列】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 思路 先生成全排列,然后判断哪些情况不符合的,剔除就好了 ...
- Android Weekly Notes Issue #316
July 1st, 2018 Android Weekly Issue #316 本期内容包含教你使用Kotlin通过Annotation Processor生成代码文件, JetPack中的Andr ...
- 升级GCC 6.2编译LLVM的问题
[ 55%] Built target RTInterception.x86_64 [ 55%] Building ASM object projects/compiler-rt/lib/saniti ...
- UVA11426 GCD - Extreme (II) —— 欧拉函数
题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...
- interceptors
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean ...
- CSS+HTML+JQuery简单菜单
1. [代码]style <style type="text/css"> body,ul,li,a{ margin:0; paddin ...
- listen 74
Genes Link Touch and Hearing Sound and touch may seem completely separate, except possibly when play ...