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的更多相关文章

  1. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  2. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  3. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  4. [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 ...

  5. 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 ...

  6. 【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 ...

  7. 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 ...

  8. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  9. 【一天一道LeetCode】#34. Search for a Range

    一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...

  10. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

随机推荐

  1. func_get_args func_num_args 的使用

    func_get_args是获取方法中参数的数组,返回的是一个数组,与func_num_args搭配使用: func_num_args一般写在方法中,用于计数 function eeee($a='gg ...

  2. C语言:标准IO_fopen( )、fclose() ①

    思前想后一个月,我终于敲下了我的第一篇开山之作. 博客千千万,我的博客首先记录的是学习时候的理解,用于给自己翻阅查找,现在主要研究的是C语言和STM32.如果能帮到你,那是最好的,假如我写的东西有错误 ...

  3. IBM DS5020 管理口密码重置

    IBM DS5020 管理口密码重置 使用超级终端进行连接,输入回车,然后Ctrl+Break(有时需要多按几次),屏幕会出现设置波特率的提示: Send for shell access or ba ...

  4. 牛客练习赛47 A DongDong破密码 (异或性质,递推)

    链接:https://ac.nowcoder.com/acm/contest/904/A 来源:牛客网 DongDong破密码 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1310 ...

  5. docker命令(随时补充)

    导入导出容器:https://blog.csdn.net/LEoe_/article/details/78685156 拷贝文件到容器内:docker cp 本地路径 容器长ID:容器路径

  6. POSTGRESQL 批量权限 管理方法

    原博地址 https://yq.aliyun.com/articles/41512?spm=a2c4e.11153940.0.0.20b7640fcDiFQA 关于PostgreSQL的逻辑架构和权限 ...

  7. Java并发编程实战 第14章 构建自定义的同步工具

    状态依赖性 定义:只有满足特定的状态才能继续执行某些操作(这些操作依赖于固定的状态,这些状态需要等待别的线程来满足). FutureTask,Semaphroe,BlockingQueue等,都是状态 ...

  8. Luogu P3886 [JLOI2009]神秘的生物 最小表示法,轮廓线DP,插头DP,动态规划

    亲手写掉的第一道最小表示法!哈哈哈太开心啦~ 不同于以往的几个插头\(dp\),这个题目的轮廓线是周围的一圈\(n\)个格子.而其所谓"插头"也变成了相邻格子的所属连通分量编号,并 ...

  9. Linux使echo命令输出结果带颜色

    echo -e "\033[30m 黑色字 \033[0m"echo -e "\033[31m 红色字 \033[0m"echo -e "\033[3 ...

  10. python基础31[python IDE之Eclipse+PyDev]

    一 入门IDE作为python的初学者,在语法和类库学习阶段,我们可以使用以下简单使用的IDE:1) Python SDK 自带的IDEL(Python GUI)2) Komodo-Edit3) No ...