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: 在已知递减排序的数组中,查找到给定 ...
随机推荐
- JavaSE基础:泛型
泛型 1.引入 情景模式描述,假设完成一个学生的成绩的情况: 整数: math=80,english=70 小数: math=85.6,englisth=77.8 字符串: math="66 ...
- 自己对GIS的思考
这只是我自己的理解,谈不上对整个行业的理解,只能从自己的角度谈谈GIS,谈谈爱和恨. 现在在武汉的一所所谓的全国GIS数一数二的学校里面读硕士,从高中开始我就很喜欢地理学科,大学选择了地球信息科技这个 ...
- asp.net WebApi WebApiConfig.cs Web API 配置和服务
public static void Register(HttpConfiguration config) { ............................... var jsonSett ...
- Big Data(五)关于Hadoop的HA的实践搭建
JoinNode 分布在node01,node02,node03 1.停止之前的集群 2.免密:node01,node02 node02: cd ~/.ssh ssh-keygen -t dsa -P ...
- Linux抓包与扫描工具
一.nmap扫描工具介绍: 1.安装nmap,如下: 2.检查目标主机所开启的TCP服务: 3.检查x.x.x.x/24网段内哪些主机开启了FTP.SSH服务 二.使用tcpdump分析 1.执行FT ...
- 格兰杰因果 Granger causality
格兰杰因果关系(Granger causality )是基于预测的因果关系统计概念.根据格兰杰因果关系,如果信号X1“格兰杰Causes”(或“G-Causes”)信号X2,则X1的过去值应该包含有助 ...
- Vue基础第二章
1.数据绑定与数据声明 Vue中的数据绑定就是让与Vue实例绑定的DOM节点或script标签内的变量之间数据更新互相影响,即数据绑定后Vue实例的数据修改会使DOM节点的数据或者script标签内的 ...
- 【学习】024 springCloud
单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N个子 ...
- Eclipse/MyEclipse超全常用快捷键汇总,绝对实用
[MyEclipse CI 2019.4.0安装包下载] Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 常用快捷 ...
- java常用类与包装类--包装类
2.基本数据类型数据的包装类 局部变量中基本数据类型直接分配在栈中,而对象分配在堆中 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法来操作该数据 包装类主要功能:用于基本数据类型与字 ...