题目要求: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. SPOJ16607 IE1 - Sweets

    题面 传送门: 洛咕 SPOJ Solution 这题的想法挺妙的. . 首先,对于这种区间求答案的问题,我们一般都可以通过类似前缀和的思想一减来消去a,即求[a,b]的答案可以转化为求[1,b]-[ ...

  2. 【Luogu】 P5482 [JLOI2011]不等式组 题解

    本来以为有多难,结果发现是道树状数组水题... 显然,对于每一个添加的不等式,有3种情况: \(a<0\) .此时可转换为 $x < {{a} \over {c-b}} $ . 但是,我们 ...

  3. leetcode 43:construct-binary-tree-from-inorder

    题目描述 给出一棵树的中序遍历和后序遍历,请构造这颗二叉树 注意: 保证给出的树中不存在重复的节点 Given inorder and postorder traversal of a tree, c ...

  4. 使用Python虚拟环境

    python 的虚拟环境可以为一个 python 项目提供独立的解释环境.依赖包等资源,既能够很好的隔离不同项目使用不同 python 版本带来的冲突,而且还能方便项目的发布. virtualenv ...

  5. HTML5+CSS3 QQ会员页面导航

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. TPE-ThreadPoolExecutor

    TPE: java.util.concurrent.ThreadPoolExecutor public ThreadPoolExecutor(int corePoolSize, int maximum ...

  7. TypeError: react__WEBPACK_IMPORTED_MODULE_2___default.a.createClass is not a function

    在看阮一峰的react入门的时候,写到一段代码,但是写完就报错了,经过多方查找,终于解决掉了 错误描述: 解决方法: 将React.createClass换成React.Component, 但是不知 ...

  8. custom-ubuntu-server-iso

    Remastering the Ubuntu Desktop ISO is easy considering the existing graphical tools but did you ever ...

  9. CTDB与LVS搭建集群

    搭建一个采用lvs进行负载均衡的CTDB集群,整个集群的架构是采用如图所示 在上图所示的架构图中,后端采用的集群是我们的存储,集群存储的三个samba服务器的node在作为CTDB的节点的同时,也是运 ...

  10. OMV openmediavault NAS系统命令显示颜色

    闲鱼65f元买的我家云刷了OMV系统. 但ls命令查看文件不显示颜色. cd /etc/进入配置文件目录查看并没有bashrc文件,但有个bash.bashrc 在 bash.bashrc后面加入以下 ...