leetcode -- Search for a Range -- 思路简单,要再做一遍
2015-12-14 20:54
477 查看
https://leetcode.com/problems/search-for-a-range/
参考
/article/4981605.html
思路 就是BS mid找到target之后,再左右搜索边界即可.
参考
/article/4981605.html
思路 就是BS mid找到target之后,再左右搜索边界即可.
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ left = 0; right = len(nums) - 1 while left <= right: mid = (left + right) / 2 if nums[mid] > target: right = mid - 1 elif nums[mid] < target: left = mid + 1 else: list = [0, 0] #这里注意加上下面这两句,对应的case是[2,2],target = 2 if nums[left] == target: list[0] = left if nums[right] == target: list[1] = right for i in range(mid, right+1): if nums[i] != target: list[1] = i - 1; break for i in range(mid, left-1, -1): if nums[i] != target: list[0] = i + 1; break return list return [-1, -1]
相关文章推荐
- swift !?
- 设计模式-装饰模式
- 由“Java中一个字符占两个字节”引起
- 计算闰年_winform
- 浏览器-08 chromium 渲染2
- MyBatis 插入空值时,需要指定JdbcType
- Codeforces Round #335 (Div. 2) A.Magic Spheres
- 在Hyper-V vNext中使用检查点
- 依赖注入的javascript实现
- 注册验证码校验思路
- #三分法判断单峰函数最值#附加例题LA 5009
- 判断字符串里面的左右自负是否被+包围
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
- ubuntu14 简单安装ffmpeg
- Cocos3D专题课程源码地址
- 自定义View步骤
- GPU(CUDA)学习日记(九)------ CUDA存储器模型
- Android:SystemClock 随记
- 数据结构实验之查找一:二叉排序树【OJ--3373】
- 使用vs调试unity脚本的方法