题意

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

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,

Given [5, 7, 7, 8, 8, 10] and target value 8,

return [3, 4].

给定一个有序数组和一个目标值,查找出目标值在数组中出现的发生下标区域。时间复杂度要求log(N)。

解法

刚开始以为下标也需要用二分来查找,但看讨论发现大家都是找出目标值然后再向两边搜索-_-b,这样做的话,最差的情况下(数组里的数全都一样)就是O(N)了,但是也能过。

class Solution
{
public:
vector<int> searchRange(vector<int>& nums, int target)
{
int left = 0;
int right = nums.size() - 1;
int index_a = -1;
int index_b = -1;
int index = 0;
bool flag = false; while(left <= right)
{
int mid = (left + right) >> 1;
if(nums[mid] == target)
{
flag = true;
index = mid;
break;
}
else
if(nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
if(flag)
index_a = index;
while(index_a >= 1 && nums[index_a - 1] == target)
index_a --;
if(flag)
index_b = index;
while(index_b < nums.size() - 1 && nums[index_b + 1] == target)
index_b ++; vector<int> rt = {index_a,index_b};
return rt;
}
};

LeetCode Search for a Range (二分查找)的更多相关文章

  1. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  2. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

  3. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

  4. [LeetCode] Search for a Range 搜索一个范围

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

  5. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

  6. Leetcode Search for a Range

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

  7. Leetcode 278 First Bad Version 二分查找(二分下标)

    题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...

  8. LeetCode First Bad Version (二分查找)

    题意: 有一个bool序列表示对应下标的版本是否出问题(下标从1开始),如果一个版本出了问题,那么其后面全部版本必定出问题.现在给出判断任意版本是否出问题的API,请找到第一个出问题的版本. 思路: ...

  9. [LeetCode] Search for a Range [34]

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

随机推荐

  1. docker部署nginx,并实现负载均衡。

    安装与使用 安装 nginx官网下载地址 发布版本分为 Linux 和 windows 版本. 也可以下载源码,编译后运行. 从源代码编译 Nginx 把源码解压缩之后,在终端里运行如下命令: $ . ...

  2. Oracle EBS OPM 取消生产批

    --取消生产批 --created by jenrry SET serveroutput on; DECLARE p_batch_header_rec gme_batch_header%ROWTYPE ...

  3. excel表格中添加单引号的方法

    今天碰到需要插入大量数据的excel表格,其中有很多文本,需要添加单引号. 方法如下: 左边是原始数据,右边是我即将添加单引号的空白区域. 第一步:在需要添加的位置输入= 第二步:输入等号之后点击需要 ...

  4. python 多进程和多线程的区别

    了解线程和进程 进程 程序:磁盘上的可执行二进制文件,并无运行状态. 进程:就是一个正在运行的任务实例(存活在内存里). 获取当前电脑的CPU核心数: pip install psutil >& ...

  5. Linux学习之路-2017/12/22

    第三章  管道符.重定向与环境变量 管道命令符,“|”,作用是将前一个命令的标准输出当作后一个命令的标准输入, 格式:“命令A|命令B” 输入输出重定向, 标准输入,STDIN,文件描述符为0,默认从 ...

  6. C++类中的特殊成员函数-------复制构造函数

    在C++中存在一个特殊函数,他就是复制构造函数,假如我们有类A,如果有A a;A b=a;   在这种情况下利用A类变量a来给同是A类变量的b来赋值,这个时候类会使用复制构造函数,如果我们不显式声明复 ...

  7. 洛谷 P4707 【重返现世】

    题目分析 题目就是求第K种原料的出现期望时间. 考虑广义min-max容斥. \(\text{kthmax}(S)=\sum\limits_{T\subseteq S}(-1)^{|T|-k}\bin ...

  8. python第三十课--异常(raise关键字)

    演示: 1.手动抛出异常对象-->raise关键字 2.try-except代码不能解决语法错误 try: print('try...') raise TypeError('类型有误的异常') ...

  9. 21天,搞定软件测试从业者必备的Linux命令

    开始之前,先同步一个结论: 对于软件测试从业者,如果你至今为止,还不懂Linux,或者完全没有接触Linux ,这是一件很危险和恐怖的事 . 此刻.现在.果断,学习Linux命令 . 如果你工作中,完 ...

  10. JAVA框架 Spring AOP注解

    一.准备工作: 1)导入jar包: 4个jar包. 2)约束:(spring需要所有的约束)有IOC约束和AOP 还有事务(tx)以及注解注入的约束(context). <?xml versio ...