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. ...
随机推荐
- python用scrapy模拟用户登录
scrapy模拟登录 关注公众号"轻松学编程"了解更多. 注意:模拟登陆时,必须保证settings.py里的COOKIES_ENABLED(Cookies中间件) 处于开启状态 ...
- GitHub 上适合新手的开源项目(Python 篇)
作者:HelloGitHub-卤蛋 随着 Python 语言的流行,越来越多的人加入到了 Python 的大家庭中.为什么这么多人学 Python ?我要喊出那句话了:"人生苦短,我用 Py ...
- 5 MVVM
1.概述 MVVM各个部分功能如下: Model:定义业务逻辑 View:定义面向用户接口,UI逻辑,处理用户交互请求 ViewModel:负责界面导航逻辑和应用状态管理,呈现逻辑. 1.1. 各司其 ...
- 我叫Mongo,干了「查询终结篇」,值得您拥有
这是mongo第三篇"查终结篇",后续会连续更新5篇 mongodb的文章总结上会有一系列的文章,顺序是先学会怎么用,在学会怎么用好,戒急戒躁,循序渐进,跟着我一起来探索交流. 通 ...
- 1redis介绍
一,概述 是一种nosql数据库,保存在内存中,同时redis可以把内存同时保存到磁盘,即可以把数据持久化.支持较多的数据类型,string,list(队列和栈),set,sorted set,has ...
- 安装使用Pycharm及Anaconda最全教程
网上安装anaconda和pycharm的教程很多,然而很少有人能够很详细地讲解,特别是对于pycharm的虚拟环境相关的说明很少,我也是懵逼的用了两年多,经常发现之前pycharm安装的第三方库,明 ...
- Elasticsearch 第七篇:父子结构mapping设计以及相关查询
h2.post_title { background-color: rgba(43, 102, 149, 1); color: rgba(255, 255, 255, 1); font-size: 1 ...
- Oracle表和表空间查询
用户查询 查询和用户相关的数据 创建用户 CREATE USER user IDENTIFIED BY password [DEFAULT TABLESPACE tablespace] [TEMPOR ...
- wget 快速下载 ftp 文件
GNU Wget 1.17.1,非交互式的网络文件下载工具. 用法: wget [选项]... [URL]... 长选项所必须的参数在使用短选项时也是必须的. 启动: -V, --version 显示 ...
- Cypress系列(90)- Cypress.Cookies 命令详解以及如何跨测试用例共享 Cookies
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html Cypress.Cookies 共有三个 ...