Leetcode-34-Search for a Range-(Medium)
这道题借助二分查找算法来查找目标值的index
然后向前和向后分别搜索起始边界
注意开始排除异常值优化速度
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'author' class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
length = len(nums)
if len(nums) == 0 or nums[0] > target or nums[length - 1] < target:
return [-1, -1]
index = self.binary_search(nums, 0, length - 1, target)
if index == -1:
return [-1, -1]
else:
start = index
end = index
for i in range(index - 1, -1, -1):
if nums[i] == target:
start = i
else:
break
for i in range(index + 1, length):
if nums[i] == target:
end = i
else:
break
return [start, end] #二分查找算法
def binary_search(self, nums, start, end, targrt):
if start > end:
return -1
mid = start + (end - start)//2
if nums[mid] > targrt:
return self.binary_search(nums, start, mid - 1, targrt)
elif nums[mid] < targrt:
return self.binary_search(nums, mid + 1, end, targrt)
else:
return mid
另外的一种思路是寻找 target-1 和 target+1的所在位置的索引,这两个值可能不存在,那么需要相应的修改二分查找算法
Leetcode-34-Search for a Range-(Medium)的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [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 ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
随机推荐
- beanutils中类型转换
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Servl ...
- 关于iTunes随机播放和我所不知道的自己
无意中看到这套题,很有意思,自己做了一下. 规则是这样的:打开你的播放器,我的是iTunes,不管是哪个,总之打开最全的那个播放列表,开启随机播放,按顺序把每首歌名写在下面每道题的后面,比如第一首歌是 ...
- Jquery框架
现在Jquery框架对于开发人员基本上是无人不知,无人不晓了,用起来十分的方便,特别是选择器十分强大,提高了我们的开发速度.但是好多人也只是停留在了会用的基础上,我个人觉得会用一个框架不算什么,只能说 ...
- 【转】在iOS开发中使用FMDB
本文转载自:唐巧的博客 在iOS开发中使用FMDB APR 22ND, 2012 前言 SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iO ...
- mongo查询某个字段是否存在,并删除记录里的这个字段
查询course表中,存在lectures_count字段的记录信息 db.course.find( { "lectures.lectures_count": { $exists: ...
- JVM内存划分
JVM内存划分吗? 前言: 大家都知道虚拟机,都知道JVM,其实这些都是基于sun公司[oracle公司]的HotSpot虚拟机,当然本篇博文也是以sun公司为基础.还有其他的虚拟机,常见的就有JRo ...
- [整理]在命令行执行 UIAutomation
instruments -t /Developer/Platforms/iPhoneOS.platform/Developer/Library/Instruments/PlugIns/Automati ...
- SOCKET网络编程5
SOCKET网络编程快速上手(二)——细节问题(5)(完结篇) 6.Connect的使用方式 前面提到,connect发生EINTR错误时,是不能重新启动的.那怎么办呢,是关闭套接字还是直接退出进程呢 ...
- IceMx.Mvc 我的js MVC 框架 开篇
开篇 这篇文章是后补的,前端时间想写一些对于js开发的一些理解,就直接写了,后来发现很唐突,所以今天在这里补一个开篇. 我的js Mvc 框架 基于实用设计,过分设计等于没设计.本着简单的原则,它只实 ...
- 【总结】AngularJs学习总结
应项目的需要,一个月之前开始做WebComponents.Javascript MVC框架的技术调研,由于重点是想做组件化,所以就没有考虑Backbone(去年就小试牛刀,太难用了)及其他的mvc框架 ...