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: 在已知递减排序的数组中,查找到给定 ...
随机推荐
- Elasticsearch入门教程(四):Elasticsearch文档CURD
原文:Elasticsearch入门教程(四):Elasticsearch文档CURD 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...
- C# System.Reflection (反射)
在使用.NET创建的程序或组件时,元数据(metadata)和代码(code)都存储于“自成一体”的单元中,这个单元称为装配件.我们可以在程序运行期间访问这些信息. 在System.Reflectio ...
- CSS hack(过滤器)
CSS hack概念: 是针对不同浏览器对同一段代码解析不同的处理方案:<解决兼容性问题> 属性设置在不同版本的IE里会出现不兼容问题,css hack解决兼容主流浏览器和IE 常见的过滤 ...
- 设备树里面#address-cells 、#size-cells、reg三者的关系
栗子1: cpus { #address-cells = <>; #size-cells = <>; cpu@ { compatible = "arm,cortex- ...
- KVM虚拟化简介及安装
kvm是基于图形化的linux操作的 安装图形化界面的知识点: 磁盘空间有两个词: 精简置备:我先在我系统里面去声明我要一个50G的空间,但是呢,我不会把50G都分给你,你用多少,我分给你多少,但是做 ...
- 遍历获取html页面所有元素的id
因为要写一个权限管理系统精确到每个标签.以前用过的是给每个标签加权限判断的代码.但感觉这种方式太麻烦和落后,向让页面加载时自动把每个标签的id扫描到并保存到数据库.通过id控制.研究了下实现代码如下: ...
- PAT Basic 1042 字符统计 (20 分)
请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包含 1 个英文字母, ...
- 索引介绍,转载自:https://tech.meituan.com/2014/06/30/mysql-index.html
索引原理 除了词典,生活中随处可见索引的例子,如火车站的车次表.图书的目录等.它们的原理都是一样的,通过不断的缩小想要获得数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是我们总 ...
- 关于PHP://input
$data = file_get_contents("php://input"); php://input 是个可以访问请求的原始数据的只读流. POST 请求的情况下,最好 ...
- Django学习系列3:创建仓库
在创建仓库之前,在项目superlists中新建一个Python文件,命名为functional_tests.py,里面的内容如下: # File: functional_test.py # Auth ...