leetcode Search for a Range python
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:
rs=[0,0]
if nums[left] == target:
rs[0] = left
if nums[right] == target:
rs[1] = right
for i in range(mid,right+1):
if nums[i] != target:
rs[1] = i-1
break
for i in range(mid,left-1,-1):
if nums[i] != target:
rs[0] = i + 1
break
return rs
return [-1,-1]
leetcode Search for a Range python的更多相关文章
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- iOS音频播放(二):AudioSession
(本文转自码农人生) 前言 在实施前一篇中所述的7个步骤步之前还必须面对一个麻烦的问题,AudioSession. AudioSession简介 AudioSession这个玩意的主要功能包括以下 ...
- IOC原理分析
IOC(inversion of control)控制反转 在我们的程序中,要实现某个功能,我们都会用到两个或两个以上的类来协同完成,那么在一个类中,我们就会要有它的合作类的引用,也就是说这个类依赖于 ...
- codeblocks 使用指南z
1.界面风格更改 首先贴怎么普通设置出来,或者改配置文件 这是我的风格 类似于DEV-CPP里面的一个主题,看的很舒服 具体设置如下: Settings-Editor 1.代码当前行高亮 在Gener ...
- 怎么在一个list集合里面筛选重复的数据,在重复的数据中取最后添加的那条数据
1.先将集合进行分组(分组字段)2.在判断分组的数量是否大于 03.大于0,则有重复的数据
- JQuery 拖动层
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- ajax读取txt文件
注意url为网络路径 <html><head><script type="text/javascript"> function loadXML ...
- 修改已经提交到远端的git commit信息
有这么一种场景,就是以前没有设置用户名和邮箱,导致提交时git commit信息中用户信息不正确.这样的情况导致后来我们提交代码到git上面时因为身份验证错误,只有到 push 阶段了才发现提交不上去 ...
- Gradle templates 的使用
使用gradle 时有一些繁琐的创建工程 使用插件的步骤. 这些步骤可以使用一些gradle的模板来代替. 具体的操作步骤: 安装gradle: Prerequired: Java SDK insta ...
- python 网络编程第三版
为服务端增加多线程解决方案 1.服务端代码如下: ***这个版本并没有真正的起到多线程的作用,主要原因在于t.join():以后的版本会改进这个问题*** #!/usr/bin/python #!co ...
- inux网卡与MAC地址绑定方法总结
使用linux系统时会出现这样的情况,当你安装了某个网卡的驱动程序时,或者安装了与网卡相关的程序后. 网卡会出现所谓的漂移现象.(注意:不是飘逸).可能的表象为: (1):网卡顺序颠倒,比如之 ...