LeetCode 034 Search for a Range
题目要求: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之前必须确定序列为有序序列,否则调用出错。
代码如下:
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的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- 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 ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [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 ...
- 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 ...
- 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 ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- leetcode 【 Search for a Range 】python 实现
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- 正式班D20
2020.11.02星期五 正式班D20 目录 11 软件包管理 11.1 软件包介绍 11.1.1 编程语言分类 11.1.2 三种安装包 11.2 rpm包管理 11.2.1 rpm包简介 11. ...
- 【Postman】使用Postman实现接口数据关联
首先下载安装Postman直接打开官网,点击下载按钮即可完成下载https://www.getpostman.com/downloads/ 栗子业务场景:用户登录医生账户,查询自己的处方列表数据:用户 ...
- 妙用 Intellij IDEA 创建临时文件,Git 跟踪不到的那种
| 好看请赞,养成习惯 你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it ...
- 快快使用ModelArts,零基础小白也能玩转AI!
摘要: 走过路过不要错过,看Copy攻城狮如何借力华为云ModelArts玩转AI. "自2018年10月发布以来,ModelArts累计服务了众多行业十几万开发者,通过基础平台的完备性和面 ...
- C#5语法新特性
C# 5.NET Framework 4.5 Visual Studio 2012 C#5.0新引进的语法基于.Net Framework 4.5.主要引进的语法:异步与等待,调用方信息 其中最重要的 ...
- Markdown tricks
编辑排版 仅仅了解 Markdown 语法还不够,知道这些 排版技巧 增色您的文章内容. 空格和空行 留白,从艺术角度上说,留白就是以"空白"为载体进而渲染出美的意境的艺术.从应用 ...
- Java程序员成长之路
北哥在前文总结了程序员的核心能力,但在专业能力维度,只是做了大概的阐述,并没有详细展开.从今天开始,我会把我作为程序员成长过程中,学习的知识总结成系列文章陆续发出来,供大家学习参考. 本文是第一篇,关 ...
- leetcode64:maximal-rectangle
题目描述 给出一个只包含0和1的二维矩阵,找出最大的全部元素都是1的长方形区域,返回该区域的面积. Given a 2D binary matrix filled with 0's and 1's, ...
- tp3.2 php sdk上传七牛云
//获取上传token Vendor('sdk.autoload'); $accessKey='********'; $secretKey='*******'; $auth=new \Qiniu\Au ...
- linux netfilter rule match target 数据结构
对于netfilter 可以参考 https://netfilter.org/documentation/HOWTO/netfilter-hacking-HOWTO-3.html netfilter ...