题意

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. [CentOS7] [VMWARE] 增加磁盘空间后扩大逻辑分区

    Learn to rescan disk in Linux VM when its backed vdisk in vmware is extended. This method does not r ...

  2. 修改SQL Server数据库表的创建时间最简单最直接有效的方法

    说明:这篇文章是几年前我发布在网易博客当中的原创文章,但由于网易博客现在要停止运营了,所以我就把这篇文章搬了过来,因为这种操作方式是通用的,即使是对现在最新的SQL Server数据库里面的操作也是一 ...

  3. Windows API串口编程详解

    (一)Windows API串口通信编程概述 Windows环境下的串口编程与DOS环境下的串口编程有很大不同.Windows环境下的编程的最大特征之一就是设备无关性,它通过设备驱动程序将Window ...

  4. leetcode 1. Two Sum [java]

    注意点: HashMap<Integer, Integer> return new int[]{}; 3 2 4 target:6 return null; public int[] tw ...

  5. 2.python数据结构的性能分析

    一.引言 - 现在大家对 大O 算法和不同函数之间的差异有了了解.本节的目标是告诉你 Python 列表和字典操作的 大O 性能.然后我们将做一些基于时间的实验来说明每个数据结构的花销和使用这些数据结 ...

  6. table中怎么设置两行间距

    table中设置两行间距方法: css代码实现 <style> table   {   border-collapse:   separate;   border-spacing:   1 ...

  7. 魔法少女【动态规划问题】——NYOJ1204

    个人博客页:https://www.scriptboy.cn/202.html 题目描述: 前些时间虚渊玄的巨献小圆着实火了一把. 在黑长直(小炎)往上爬楼去对抗魔女之夜时,她遇到了一个问题想请你帮忙 ...

  8. centos 7 安装jira 破解

    http://blog.csdn.net/itjinglun/article/details/52240479

  9. Python2.7-re模块

    re模块 1.正则表达式的语法 '.' 匹配任意字符,若指定了re.S,则可以匹配换行符'^' 匹配行首,即字符串的开头,若指定了re.M,会自动匹配每行开头'$' 匹配行尾,同上'*' 匹配大于等于 ...

  10. python下载安装搭建

    python官网下载python运行环境(https://www.python.org/downloads/),建议下载稳定版本,不推荐使用最新版本 安装 然后我们打开CMD,在里面输入python, ...