题目要求: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. Java的Arrays.sort()方法到底用的什么排序算法

    暂时网上看过很多JDK8中Arrays.sort的底层原理,有些说是插入排序,有些说是归并排序,也有说大于域值用计数排序法,否则就使用插入排序...其实不全对.让我们分析个究竟: 1 // Use Q ...

  2. python开发基础(二)运算符以及数据类型之tuple(元组)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  3. 【洛谷】P1009 阶乘之和——高精度算法

    题目描述 用高精度计算出S = 1! + 2! + 3! + - + n!  ( n ≤  50 ) S = 1! + 2! + 3! + - + n! ( n ≤ 50 ) 其中"!&qu ...

  4. .NET redis 客户端开源组件 FreeRedis (继 CSRedisCore 之后重写)

    什么是 FreeRedis FreeRedis 是一款 .NET redis 客户端开源组件,以 MIT 协议开源托管于 github,目前支持 .NET 5..NETCore 2.1+..NETFr ...

  5. Linux系统下安装配置JDK(rpm方式及tar.gz方式)

    以前都是在Windows环境进行开发的,最近因工作需要:学习在Linux系统下搭建开发环境,自此记录搭建过程,以方便查阅. 本文借鉴了 Angel挤一挤 .小五 两位的博客. 准备材料: JDK下载链 ...

  6. 【开发板试用报告】鸿蒙OS环境搭建及代码烧录

    鸿蒙系统的代码编译环境需要linux系统,软件开发和代码烧录需要windows环境. Linux环境 参考官方链接:https://gitee.com/openharmony/docs/blob/ma ...

  7. javascript播放带透明通道的mp4动画

    随着互联网的发展,动画效果也在一直更新,从刚开始的flsh动画,cocos骨骼动画,到YY开源的svga动画.最近1年来,带有透明通道的mp4动画被使用的极为广泛,对于app端.github上有开源的 ...

  8. 内网渗透 day11-免杀框架

    免杀框架 目录 1. venom框架 2. shelltel框架 3. backdoor factory(BDP) 1. venom框架 cd venom进入venom文件夹中./venom.sh进入 ...

  9. RPC协议实践入门

    RPC 是什么 RPC(Remote Procedure Call) 是一个计算机通信协议.该协议允许运行与一台计算机的程序调用另一个地址空间的程序,是一个通过发送请求-接受回应进行信息交互的系统. ...

  10. ubutun下安装phantomjs配置chromedriver

    我们在进行动态html页面获取时候,需要用到selenium,phantomjs,chromedriver selenium可以通过命令python的pip进行安装, phantomjs与chrome ...