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()和upper_bound()两个函数用一下就好。

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int>ans;
int n=nums.size();
int a=lower_bound(nums.begin(),nums.end(),target)-nums.begin();
if((a==n)||(nums[a]!=target)){
ans.push_back(-);
ans.push_back(-);
return ans;
}
else{
int b=upper_bound(nums.begin(),nums.end(),target)-nums.begin();
ans.push_back(a);
ans.push_back(b-);
return ans;
}
}
};

leetcode 34 Search for a Range(二分法)的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

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

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

  4. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  5. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

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

  7. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  8. [leetcode 34] search for a range

    1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  9. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

随机推荐

  1. COM线程模型 套间概念

    COM线程模型 套间概念 1) 单线程套间.线程是COM主线程,初始化COM库的进程的第一个线程.即使从其他线程访问COM组件,也不需要手工同步,COM库已经实现了同步.寓所线程里有个消息处理循环来处 ...

  2. 简洁的一键SSH脚本

    这里发一个自己图省事搞的一个批量打通SSH的脚本,可能对于好多朋友也是实用的,是expect+python的一个组合实现,原理非常easy, 使用起来也不复杂,在此还是简单贴出来说说. noscp.e ...

  3. Mysql主从复制,实现数据同步

    大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢 ...

  4. asp.net mvc 反射应用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Unable to determine IP address from host name

  6. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  7. Basis 基础

    [转自 http://www.cnblogs.com/elegantok/archive/2008/11/03/1325163.html] 1 Basis1. Unix操作 更改口令 passwd e ...

  8. Django——form组件is_valid校验机制

    #先来归纳一下整个流程#(1)首先is_valid()起手,看seld.errors中是否值,只要有值就是flase#(2)接着分析errors.里面判断_errors是都为空,如果为空返回self. ...

  9. 使用documentFragment

    function insertHtml(range, val) { var doc = range.doc, frag = doc.createDocumentFragment(); K('@' + ...

  10. python打包工具 --- pyinstaller

    安装 安装python并添加到环境变量之后,在终端执行如下命令即可: pip install pyinstaller 截图如下: 若安装失败,可到: https://www.lfd.uci.edu/~ ...