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. Hardwood Species

    http://poj.org/problem?id=2418 #include<cstdio> #include<cstring> #include<string> ...

  2. hdu 4649 Professor Tian 反状态压缩+概率DP

    思路:反状态压缩——把数据转换成20位的01来进行运算 因为只有20位,而且&,|,^都不会进位,那么一位一位地看,每一位不是0就是1,这样求出每一位是1的概率,再乘以该位的十进制数,累加,就 ...

  3. Android SlidingMenu的getSupportActionBar无法找到的解决

    getSupportActionBar()方法不能用 进入Library中的src下找到SlidingFragmentActivity.java,修改 public class SlidingFrag ...

  4. live555源码研究(五)------DynamicRTSPServer类

    一.类DynamicRTSPServer作用 1,提供RTSP服务 二.类DynamicRTSPServer继承关系图

  5. php Laravel windows安装

    先要装一个composer, 如果不会装请看这里composer windows可以直接下载composer-setup.exe, 如果安装失败提示"Unable to connect to ...

  6. 如何利用python使用libsvm

    一:libsvm包下载与使用:      LIBSVM是台湾大学林智仁(Lin Chih-Jen)副教授等开发设计的一个简单.易于使用和快速有效的SVM模式识别与回归的软件包,他不但提供了编译好的可在 ...

  7. 简单的神经元算法实现(python)

    参考python代码如下 #perceptron x=[[1 ,0, 0],[1,0,1],[1, 1, 0],[1, 1, 1],[0,0,1],[0,1,0],[0,1,1],[0,0,0]] y ...

  8. Flash Builder 4.6 找不到所需的Adobe Flash Player

    问题: 安装完Flash Builder 4.6 ,第一次运行项目,出现如下错误提示: “Flash Builder 找不到所需版本的 Adobe Flash Player.您可能需要安装该版本的 F ...

  9. Java 简单的加密解密算法

    package cn.std.util; import java.nio.charset.Charset; public class DeEnCode { private static final S ...

  10. Java面试题-线程安全

    1. 什么叫线程安全?servlet是线程安全吗?       答:如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是一样的,而且其他 ...