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. ionic 手机端如何嵌入视频iframe

    需求说明:后台提供功能,可以通过富文本编辑器[summernote]上传优酷的视频链接地址(这里需要注意:优酷视频提供多种操作方式 下面截图说明,先做个标记): 客户端是通过ionic开发的:而上传的 ...

  2. Js 学习资料

    Js 语法 http://www.php100.com/manual/jquery/   jqGrid控件 http://www.trirand.com/blog/jqgrid/jqgrid.html

  3. Mysql 的一些异常解决

    一.关于大文件存储 1.利用mysql存储大文件时,异常截图 在配置文件中加上如下一行 2.改完后重启mysql,但是又报如下错误: 解决方案: 我的mysql 是5.6版本,查到网上说要修改配置文件 ...

  4. ACM/ICPC ZOJ1003-Crashing Balloon 解题代码

    #include <iostream> using namespace std; int main() { int **array = new int *[100]; for ( int ...

  5. Oracle官网下载地址大全(包括11g、10g和9i)

    Oracle11g下载: Microsoft Windows(32 位)的 Oracle Database 11g 第 2 版 (11.2.0.1.0) http://download.oracle. ...

  6. selenium IDE处理各种窗口问题解决方法

    一.处理模态窗口:showModalDialog 由于弹出模态窗口后,就无法定位到当前窗口的元素和模态窗口的元素,需要添加js解决 模态窗口动作类似下面语句: <input id="c ...

  7. UItextField常用方法

    - (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view ...

  8. Content by query webpart 自定义样式的使用方法

    今天研究一个非常实用的webpart 如果在office365 上 的webpart中一直没“内容查询”, 这里需要开启2个features:http://community.office365.co ...

  9. 接口(工厂模式&代理模式)

    程序1:简单的接口功能 package com.liaojianya.chapter2; /** * 编写程序实现一个usb接口,定义设备来使用这个接口,从而计算机可以调用具有usb接口的设备. * ...

  10. 【CF493E】【数学】Vasya and Polynomial

    Vasya is studying in the last class of school and soon he will take exams. He decided to study polyn ...