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. Hashtable和HashMap类

    Hashtable和HashMap类有三个重要的不同之处. 第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现 ...

  2. Ubuntu 12.04安装PPTP

    1.安装软件 sudo apt-get install pptpd ufw 2.编辑/etc/ppp/pptpd-options 找到 refuse-pap refuse-chap refuse-ms ...

  3. java内存

    java内存分为四部分: 1).栈区(stacksegment),由编译器自动分配释放,存放函数的参数值和局部变量的值等,具体方法执行结束之后,系统自动释放JVM内存资源: 2).堆区(heapseg ...

  4. OC加强-day04

    #pragma mark 00知识回顾 //定义一个函数 函数没有返回值函数有一个参数:返回值是double 参数是两个int的block void test(int a); void test(do ...

  5. C#微信开发之旅--自定义菜单

    上一篇说道基本信息的回复<C#微信开发之旅--基本信息的回复>,当中就说到文本信息的回复,其他信息的回复,可以参考下开发文档中回复信息的格式进行修改就可以. 下面来实现下自定义菜单.据我了 ...

  6. [jquery] jQuery点滴[持续更新]

    001.查看jquery的版本. $(function(){ console.log($()); //jquery console.log($().jquery); }); 002.(new Func ...

  7. PAT_1046 划拳

    啦啦啦.今天晚上火车回学校了.= =还是挺舍不得家里的. 题目描述: 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字 ...

  8. 九度OJ 1108 堆栈的使用

    题目地址:http://ac.jobdu.com/problem.php?pid=1108 题目描述: 堆栈是一种基本的数据结构.堆栈具有两种基本操作方式,push 和 pop.Push一个值会将其压 ...

  9. IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一

    // // WPSuggest.h // OC-API-网络访问 // // Created by wangtouwang on 15/3/24. // Copyright (c) 2015年 wan ...

  10. 一次plsql 问题记录

    环境 : window 7 x64  oracle 10.2g  plsql 10.0.5 问题是 新装的 oracle10.2 plsql 一直连接不上 ,oracle_home 配置都对 .sql ...