leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/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].
明显的,这道题需要二分查找。实际上STL上已经为我们实现好了 二分查找 函数的接口。对于“vector”容器,我们可以利用“lower_bound” 查找 目标数值 target 第一次出现的位置。如果原数组中并不存在这个 target,lower_bound会返回第一个比target值大的位置。利用lower_bound函数获取 target 在原数组的位置:auto p = lower_bound(vec.begin(), vec.end(), target);而类似的函数upper_bound则会返回第一个比target大的位置。注意:auto p 在这里是vector 迭代器类型,转换成数字下表为 idx = p - vec.begin();
代码如下:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
auto p1 = lower_bound(nums.begin(), nums.end(), target);
auto p2 = upper_bound(nums.begin(), nums.end(), target);
vector<int> res; res.clear();
if(*p1 != target) {
res.push_back(-);
res.push_back(-);
return res;
}
res.push_back(p1 - nums.begin());
res.push_back(p2 - nums.begin() - );
return res;
}
};
leetcode@ [34] Search for a Range (STL Binary Search)的更多相关文章
- stl binary search
stl binary search */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola,&q ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
Description Given a sorted array of n integers, find the starting and ending position of a given tar ...
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- Search Range in Binary Search Tree
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...
- Lintcode: Search Range in Binary Search Tree
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...
随机推荐
- uva 10730
题意:如果数列中没有三个元素的子序列构成等差数列输出yes 不然no 标记每个数出现的位置 然后从0开始寻找三个元素的等差数列 如果这三个元素的位置满足条件则原数列中存在等差数列 #include ...
- uva 10827
与108类似 多加了两层循环 水过 #include <iostream> #include <cstring> #include <cstdio> #includ ...
- Titan DB的一些问题
使用熟悉一点的系统来测试TitanDB,HBASE+ES,记录下来一些小tips. 1.首先TitanDB支持的Hadoop只有1.2.1,所以Hbase自然也只能取到0.98,虽然官网上提供了tit ...
- linux查找某个文件中单词出现的次数
文件名称:list 查找单词名称:test 操作命令: (1)more list | grep -o test | wc -l (2)cat list | grep -o test | wc -l ( ...
- hbase集群在启动的时候找不到JAVA_HOME的问题
hbase集群在启动的时候找不到JAVA_HOME的问题,启动集群的时候报错信息如下: root@master:/usr/local/hbase-/bin# ./start-hbase.sh star ...
- GridView中DataKeyNames的应用小结
一. GridView的DataKeyNames属性设为"ID,Name" GridView1.DataKeyNames = new string[]{ "ID" ...
- 在其他的电脑上配置绿色Jre+tomcat运行环境
其他的同事要使用我们的web程序(基于tomcat的web程序).所以要求是对方的电脑任何程序都不需要安装,把我们的包拷贝过去,直接执行批处理就可以运行. 经过了一番摸索,实现方式如下: 1,准备jr ...
- (原创)CityEngine 2014和ArcGIS 10.3冲突问题的解决
先卸载ArcGIS License Manager 10.3 安装ArcGIS License Manager 10.2.2 用keygen算出ArcGIS 10.3的许可,似乎本许可在ArcGI ...
- Netcat for Windows
April 10, 2009 Netcat is a simple networking utility which reads and writes data across network conn ...
- 一步一步制作yaffs/yaffs2根文件系统(一)---储备好基础知识再打
开发环境:Ubuntu 12.04 开发板:mini2440 256M NandFlash 64M SDRAM 交叉编译器:arm-linux-gcc 4.4.3点此可下载 BusyBox版本: ...