LintCode 61. Search for a Range (Medium)

LeetCode 34. Search for a Range (Medium)

class Solution {
private:
int findBound(vector<int> &A, int target, bool findLowerBound) {
int L = 0, R = A.size() - 1;
while (L <= R) {
int M = L + (R - L) / 2;
if (A[M] < target) {
L = M + 1;
} else if (A[M] == target) {
if (findLowerBound) {
R = M - 1;
} else {
L = M + 1;
}
} else {
R = M - 1;
}
}
int res = findLowerBound ? L : R;
return res >= 0 && res < A.size() && A[res] == target ? res : -1;
}
public:
vector<int> searchRange(vector<int> &A, int target) {
vector<int> v;
v.push_back(findBound(A, target, true));
v.push_back(findBound(A, target, false));
return v;
}
};

时间复杂度: O(logn)

空间复杂度: O(1)

[OJ] Search for a Range的更多相关文章

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

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

  2. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  3. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  4. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

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

  6. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

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

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

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

  9. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

随机推荐

  1. Java用DOM操作xml

    JAXP DOM方式解析XML文档实例增删改查package jiexi; import javax.xml.parsers.DocumentBuilder; import javax.xml.par ...

  2. [转]jQuery选择器总结

    该文章转载自:http://www.cnblogs.com/onlys/articles/jQuery.html jQuery的选择器那绝对最强大的,各种你想不到,原先想总结一下,没想到搜索到这个比我 ...

  3. Spring的多配置文件加载

    如果配置文件存在多个的情况下,加载配置文件的方式是:1--可以指定总的配置文件去包含子的配置文件,然后只加载总的配置文件即可在总配置文件applicationContext.xml 中引入子文件 &l ...

  4. 代码:Masonry 第三方框架

    必备宏使用前提: //define this constant if you want to use Masonry without the 'mas_' prefix #define MAS_SHO ...

  5. RS485接线 - 为什么要给2线制RS485接3根线?

    http://www.chipkin.com/rs485-cables-why-you-need-3-wires-for-2-two-wire-rs485/ RS485needs 3 conducto ...

  6. 01_JavaMail_01_邮件服务器简述

    [收发邮件简单流程] 过程大致是: 发邮件时从客户端发邮件发送到邮件服务器,收邮件就是把邮件服务器的邮件下载到客户端. [邮件协议] * SMTP:(Simple Mail Transfer Prot ...

  7. Codevs 3729 飞扬的小鸟

    飞扬的小鸟 标签 动态规划 NOIp提高组 2014 难度 提高+/省选- 题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小 ...

  8. PHP CURL参数详解

    curl用法:cookie及post 一.cookie用法 <?php $cookie_jar = tempnam('./tmp','cookie'); // login $c=curl_ini ...

  9. IOS 学习笔记 20150314

    Objective--C 类与对象 1 关键字 @interace 类定义 @end 类结束 @implementation 类实现 : 继承 @public 公用 @private 私有 @prot ...

  10. 处理safari缓存的办法

    window.onpageshow = function(event) {        if (event.persisted) {             alert("From bac ...