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 ...
随机推荐
- 使用ServletContext读取properties配置文件
创建配置文件: 1.在项目的任意地方,右键->New->File->FileName->输入->名称.properties(比如:config.properties) 2 ...
- Asp.Net Web Api 接口,拥抱支持跨域访问。
如何让你的 Asp.Net Web Api 接口,拥抱支持跨域访问. 由于 web api 项目通常是被做成了一个独立站点,来提供数据,在做web api 项目的时候,不免前端会遇到跨域访问接口的问题 ...
- 2014.3.5-C语言学习小结
知识点: 1.结构体 struct 2.联合体 union 3.枚举 4.结构.联合与函数 =========================== 结构体 思考:如果现在希望保存一个学生的信息,该如何 ...
- 迁移Model元数据设置项
.NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序) 阅读目录: 1.需求背景介绍(Model元数据设置项应该与View绑定而非ViewModel) 1 ...
- c#winform关闭窗口时触发的事件
用户关闭软件时,软件一般会给“是否确认关闭”的提示. 通常,我们把它写在FormClosing 事件中,如果确定关闭,就关闭:否则把FormClosingEventArgs 的 Cancel 属性设置 ...
- 如何避免误用分布式事务(System.Transactions.TransactionScope)
以下内容来源与:http://www.cyqdata.com/cyq1162/article-detail-54453 1:本地事务DbTransaction和分布式事务TransactionScop ...
- 清除在Windows下访问共享文件夹时的登录信息
清除在Windows下访问共享文件夹时的登录信息 在实际工作中,经常需要访问局域网内其他机子上的共享文件夹,例如\\192.168.1.100\d$ , 首次访问时,需要输入用户名和密码才可以进入,即 ...
- 读取xml并将节点保存到Excal
using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Coll ...
- XP系统安装ArcGIS10.0需要修改的一个配置
1,右击我的电脑,查看属性. 2,选择“高级”选项卡,“启动和故障恢复”单击“设置”. 3,在“默认操作系统”中单击“编辑”: 4,在弹出的boot.ini文档中把操作系统改成相应的操作系统, ...
- Android开发过程中git、repo、adb、grep等指令的使用
chown 是一条在Unix系统中用于设置文件所有者和文件关联组的命令. 需要超级用户的权限才能执行此命令.只有超级用户和属于组的文件所有者才能变更文件关联组.非特权用户(非超级用户)如需要设置关联组 ...