[OJ] Search for a Range
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的更多相关文章
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- 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 ...
- 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 ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- 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 ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- 【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 ...
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
随机推荐
- 破解SQLYog30天试用方法
开始-运行-regedit ,进入注册表,在 \HEYK_CURRENT_USER\Software\{FCE28CE8-D8CE-4637-9BC7-93E4C0D407FA}下的InD保存着SQL ...
- 20151216JqueryUI学习笔记---按钮
按钮(button) , 可以给生硬的原生按钮或者文本提供更多丰富多彩的外观. 它不单单可以设置按钮或文本,还可以设置单选按钮和多选按钮.一. 使用 button 按钮使用 button 按钮 UI ...
- SecurityException:Not allowed to start service Intent ,without permission not exported from
本来是学长以前的项目,我正在重做一遍.结果突然出现了异常,我很是不解啊,怎么莫名其妙的就出现异常了呢?我昨天用还是好好的,根本就没动过源代码.于是在网上开始了一遍又一遍的查询,有的说要加权限.有的说这 ...
- JavaScript 计算两个颜色叠加值
function multiply(rgb1, rgb2) { var result = [], i = 0; for( ; i < rgb1.length; i++ ) { result.pu ...
- iOS支付宝集成步骤;王刚韧的技术博客
- 利用switch语句计算特定的年份的月份共有几天。
//利用switch语句计算特定的年份的月份共有几天. let year =2015 let month =2 //先判断闰年中二月份的情况 ifmonth ==2 { if (year %400 = ...
- O-C相关05:方法的封装.
前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...
- JS实现回到页面顶部动画效果 2016.03.23
最近在模仿各大网站写页面样式和交互,发现好多都有回到顶部的需要,所以写了一下js,记录下来. 发现还可以添加从快到慢的动画效果和随时下拉滚动条停止滚动的功能, 参考了imooc上相关课程,最终实现JS ...
- eclipse+PyDev 中报错"scrapy.spiders.Spider" ,可用"# @UndefinedVariable"压制.
# -*- coding:utf-8 -*- ''' Created on 2015年10月22日 (1.1) 例子来源: http://scrapy-chs.readthedocs.org/zh_C ...
- 查内网虚拟机映射的公网IP
1.访问ip138.com 2.curl ifconfig.me