leetcode-mid-sorting and searching-34 Search for a Range
mycode 63.98%
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if target in nums:
start = nums.index(target)
end = start
for i in nums[start+1:]:
if i == target :
end += 1
else:
return [start,end]
return [start,end]
else:
return [-1,-1]
参考
思路:类似于二分法,先用[low,high]找到包含target的子段,再用[L,R]找到包含target的两端
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
low, high = 0, len(nums)-1
while low <= high:
mid = (low+high)//2
if nums[mid] == target:
L, R = mid, mid
while L >= low and nums[L] == target:
L -= 1
while R <= high and nums[R] == target:
R += 1
return [L+1, R-1]
if nums[low] <= target < nums[mid]:
high = mid - 1
else:
low = mid + 1
return [-1, -1]
leetcode-mid-sorting and searching-34 Search for a Range的更多相关文章
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [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] 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(二分法)
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
一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
随机推荐
- Linux 防火墙之TCP Wrappers
1.TCPWrappers 原理 Telnet.SSH.FTP.POP和SMTP等很多网络服务都会用到TCPWrapper,它被设计为一个介于外来服务请求和系统服务回应的中间处理软件. ...
- 如何修改Git已提交的日志
情况一:最后一次提交且未push 执行以下命令: git commit --amend git会打开$EDITOR编辑器,它会加载这次提交的日志,这样我们就可以在上面编辑,编辑后保存即完成此次的修改. ...
- samba服务及vsftpd服务
如何配置多个网卡 第一步: 打开设置,选择网络驱动器添加 第二步: ip a 查看网卡是否添加成功 第三步: 打开刚添加的网卡配置文件(注意,你刚添加的网卡是没有配置文件的,需要去复制一份到/etc/ ...
- Django中数据库的增删改查
本随笔使用的是pycharm专业版2019.1.3.Django==1.9.8.Python2.7 这里的Django后台使用了ORM(Object Relational Mapping),全称对象关 ...
- 如何检测指定的Windows服务是否启动
在项目中,特别是安装项目中我们经常要判断一些服务是否启动(判断SQL Server是否启动最常见),在.net中我们如何判断指定的Windows服务是否启动呢?首先要知道Windows服务的显示名称, ...
- 关于sharekey 与Open system+wep
Open_system+wep与open_system的区别在于: 对于开放系统认证,在设置时启用WEP,此时,WEP用于在传输数据时加密,对于认证没有任何作用. 抓包open_system+wep: ...
- 清北学堂提高组突破营游记day6
还有一天就结束了..QWQ 好快啊. 昨天没讲完的博弈论DP: 一个标准的博弈论dp,一般问的是是否先手赢. 博弈论最关键的问题:dp过程. 对于一个问题,一定有很多状态,每个状态可以转移到其他的一些 ...
- bootstrap-table给每一行数据添加按钮,并绑定事件
https://blog.csdn.net/mht1829/article/details/72633100 https://blog.csdn.net/qq_39215166/article/det ...
- mac安装测试myCat
参考博文https://www.cnblogs.com/chanshuyi/p/head_first_of_mycat.html 1.下载解压mycat的mac版(位置不限) 2.cd进bin文件 ...
- 【洛谷P3413】萌数
题目大意:求区间 [l,r] 内萌数的个数,其中萌数定义为数位中存在长度至少为 2 的回文子串的数字. 题解:l, r 都是 1000 位级别的数字,显然是一道数位 dp 的题目,暴力直接去世. 发现 ...