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)的更多相关文章

  1. stl binary search

    stl binary search */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola,&q ...

  2. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  3. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. [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 ...

  5. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  6. [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 ...

  7. 【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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ACM题集以及各种总结大全!

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  2. spoj 665

    直接判  没什么算法  也没什么技巧  水水~~ #include <cstdio> #include <cstring> #include <algorithm> ...

  3. MySql不同版本安装

    1.win7 64位下如何安装配置mysql-5.7.4-m14-winx64  1. mysql-5.7.4-m14-winx64.zip下载 2.解压到D:/mysql.(路径自己指定) 3.在D ...

  4. SQLite入门与分析(四)---Page Cache之事务处理(1)

    写在前面:从本章开始,将对SQLite的每个模块进行讨论.讨论的顺序按照我阅读SQLite的顺序来进行,由于项目的需要,以及时间关系,不能给出一个完整的计划,但是我会先讨论我认为比较重要的内容.本节讨 ...

  5. C++ eof()函数相关应用技巧分享

    C++编程语言中的很多功能在我们的实际应用中起着非常大的作用.比如在对文件文本的操作上,就可以用多种方式来实现.在这里我们介绍的C++ eof()函数就是其中一个比较常用的基本函数. 在使用C/C++ ...

  6. Django自定义用户认证系统Customizing authentication

    扩展已有的用户模型Extending the existing User model 有两种方法来扩展默认的User Model而不用重写自己的模型.如果你不需要改变存储在数据库中的字段,而只是需要改 ...

  7. 再谈 retain,copy,mutableCopy(官方SDK,声明NSString都用copy非retain)

    之前一直以为retain就是简单的计数器+1,copy就是重新开辟内存复制对象: 其实不是这样,原来之前的自己独自徘徊于糊涂之中. (官方SDK,对NSString属性的定义都是用copy,而不是re ...

  8. python 简单示例说明os.walk和os.path.walk的不同

    import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...

  9. 【php】对PHPExcel一些简单的理解

    这里有关于excel文件的几个概念需要跟大家说明一下,这几个概念对于我们的后续编程是很有帮助的:1.工作簿:在excel环境中用来存储数据并处理数据的文件,又称为excel文件或excel文档,   ...

  10. 流行的MySql版本

    简介 MySQL是历史上最受欢迎的免费开源程序之一.它是成千上万个网站的数据库骨干,并且可以将它(和Linux)作为过去10年里Internet呈指数级增长的一个有力证明. 那么,如果MySQL真的这 ...