题目描述

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

你的算法时间复杂度必须是 O(log n) 级别。

如果数组中不存在目标值,返回 [-1, -1]

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: [3,4]

示例 2:

输入: nums = [5,7,7,8,8,10], target = 6
输出: [-1,-1]

解题思路

利用二分查找的思想,分别找到数组中target出现的首位置和末位置,其中找首位置的步骤如下:

  • 若数组中点值小于target,继续在中点值之后的数组查找
  • 若数组中点值大于或等于target,说明前面数组中还可能有target,继续在中点值之前的数组查找
  • 最后当首位置大于末位置时,首位置即为数组中第一个大于或等于target的值
  • 若首位置上的数与target相等,则返回该位置,否则返回-1

同理可得到找末位置的步骤。

代码

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if(nums.empty())
return {-,-};
int first=findFirst(nums,target);
int last=findLast(nums,target);
return {first,last};
}
int findFirst(vector<int>& nums, int target){
int f=,l=nums.size()-;
while(f<=l){
int m=(f+l)/;
if(nums[m]<target)
f=m+;
else
l=m-;
}
if(f<nums.size()&&nums[f]==target)
return f;
return -;
}
int findLast(vector<int>& nums, int target){
int f=,l=nums.size()-;
while(f<=l){
int m=(f+l)/;
if(nums[m]<=target)
f=m+;
else
l=m-;
}
if(l>=&&nums[l]==target)
return l;
return -;
}
};

LeetCode 34. 搜索范围(search for a range)的更多相关文章

  1. 【LeetCode OJ 34】Search for a Range

    题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...

  2. LeetCode(34)Search for a Range

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

  3. LeetCode OJ:Search for a Range(区间查找)

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

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

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

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

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

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

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

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

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

随机推荐

  1. python 中的 [:-1] 和 [::-1]

    1.案例解释 a='python' b=a[::-1] print(b) #nohtyp c=a[::-2] print(c) #nhy #从后往前数的话,最后一个位置为-1 d=a[:-1] #从位 ...

  2. 一款完美代替微信小程序原生客服消息的工具:

    一.设置:无需开发,多种回复(自动+人工)   自动回复形式有3种: 打开客服消息(用户只要和客服互动过一次,再次点击进入,会收到设置好的自动回复) 关键词回复(用户在小程序中回复某个关键词内容时,会 ...

  3. 点击切换JS

    $(function(){ var tabnav = $("#tab-nav ul li"); tabnav.click(function(){ $(this).addClass( ...

  4. mysql5.7主从复制及相关注意点!

    首先在两台不同IP的服务器安装相同版本的mysql(也可以docker 用 network模式).例如 主数据库(master) 178.18.0.2 my.cnf的设置(一般在/etc/mysql/ ...

  5. linux添加开机启动项、登陆启动项、定时启动项、关机执行项等的方法

    使用chkconfig命令可以查看在不同启动级别下课自动启动的服务(或是程序),命令格式如下: chkconfig --list 可能输出如下: network         0:off   1:o ...

  6. Raspbian 2019-06-20 发布

    有关新Raspbian的信息是作为今天博客文章的一部分提供的,该帖子宣布了全新的Raspberry Pi 4: 为了支持Raspberry Pi 4,我们发布了一个全新的操作系统,基于即将发布的Deb ...

  7. 单节点oracle、ASM 详细安装步骤

    目录 1.安装环境 2.系统要求 2.1 Linux安装Oracle系统要求 1.查看RAM和交换空间以及磁盘大小 2.检查所需软件包 3.配置host和主机名 2.2修改操作系统核心参数 1.创建相 ...

  8. 代码报错--------EOFError: Compressed file ended before the end-of-stream marker was reached

    背景:运行LeNet识别CIFAR-10的图像的代码时,报错: EOFError: Compressed file ended before the end-of-stream marker was ...

  9. switch的参数类型

    switch(expr1)中,expr1是一个整数表达式,整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型 ...

  10. monkeyrunner脚本录制和回放下载

    链接:https://pan.baidu.com/s/1Kye_E9u_WXeppFMlLhr_Cg 提取码:2coy