题目要求:Search for a Range

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

分析:

lower_bound():

lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个不小于value 的值。

调用lower_bound之前必须确定序列为有序序列,否则调用出错。

iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。
 
例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,而upper_bound(2)的话,返回的就是3
 

代码如下:

class Solution {
public:
vector<int> searchRange(int A[], int n, int target) { int l = distance(A, lower_bound(A, A + n, target));
int u = distance(A, upper_bound(A, A + n, target)); //找不到该元素
if(A[l] != target)
return vector<int> {-1, -1};
else
return vector<int> {l, u - 1};
}
};

LeetCode 034 Search for a Range的更多相关文章

  1. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  2. Java for LeetCode 034 Search for a Range

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

  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】Search for a Range(middle)

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

  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 【 Search for a Range 】python 实现

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

随机推荐

  1. 阿里云app原型设计

    软件需求分析与系统设计 https://edu.cnblogs.com/campus/zswxy/2018SE 这个作业要求在哪里 https://edu.cnblogs.com/campus/zsw ...

  2. Linux 系统编程 学习:06-基于socket的网络编程1:有关概念

    Linux 系统编程 学习:006-基于socket的网络编程1:有关概念 背景 上一讲 进程间通信:System V IPC(2)中,我们介绍了System IPC中关于信号量的概念,以及如何使用. ...

  3. python实现城市气候与海洋的关系研究

    城市气候与海洋的关系研究 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 后打开浏览器输入网址http:// ...

  4. 我的 Redis 被入侵了

    好吧,我也做了回标题党,像我这么细心的同学,怎么可能让服务器被入侵呢? 其实是这样的,昨天我和一个朋友聊天,他说他自己有一台云服务器运行了 Redis 数据库,有一天突然发现数据库里的数据全没了,只剩 ...

  5. Appium+python自动化环境搭建

    一.步骤及环境 环境:Windows 7版本 64位系统(python) 1.jdk安装配置:jdk1.6.0 (64位) 2.android-sdk下载安装:android-sdk_r24.3.4- ...

  6. Matlab批量绘制图像并保存

    author:ZKe ------------------------------- 以下是一个txt文件,每行11个字段,第一个字段是日期,后面10个是用户id和对应今天发表微博数,所有字段用制表符 ...

  7. C# type对象

    新建控制台应用程序 新建一个类 class MyClass { private int id; private int age; public int numb; public string Name ...

  8. 学习.NET 挑战赛

    今天访问dot.net 网站看到了一个学习.NET 挑战赛,发现已经赛程过半了,这是一个为那些想更多地了解 C# 和 .NET 的人举办的一个完全免费的课程活动,这些模块必须在 11 月底前完成.参加 ...

  9. [MIT6.006] 8. Hashing with Chaining 散列表

    一.字典 在之前课里,如果我们要实现插入,删除和查找,使用树结构,最好的时间复杂度是AVL下的Ο(log2n),使用线性结构,最好的复杂度为基数排序Ο(n).但如果使用字典数据类型去做,时间复杂度可为 ...

  10. 聊一聊sockmap 以及ebpf

    之前聊过tcpdump 抓包原理,tcpdump使用packet 抓包,使用packet_map 完成零拷贝.但是这个零拷贝也有点假,何为假呢?从网卡到内存走的dma,哪能不能直接从dma拷贝到用户空 ...