Leetcode 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].
二分查找
第一步找到元素的起点
第二步找到元素的终点
class Solution {
public:
int lower_bound(int A[], int n, int target){
int left = , right = n-;
while(left < right){
int mid = (left+right)/;
if(A[mid] < target) left = mid+;
else right = mid;
}
if(A[left]!=target) return -;
else return left;
}
int upper_bound(int A[], int n, int target){
int left = , right = n-;
while(left <= right){
int mid = (left+right)/;
if(A[mid] > target) right = mid-;
else left = mid+;
}
if(A[right]!=target) return -;
else return right;
}
vector<int> searchRange(int A[], int n, int target) {
vector<int> res;
res.push_back(lower_bound(A,n,target));
res.push_back(upper_bound(A,n,target));
return res;
}
};
Leetcode Search for a Range的更多相关文章
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode Search for a Range (二分查找)
题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- ASP.NET中使用JqGrid完整实现
文章提纲 介绍 & 使用场景 JqGrid的一些说明 JqGrid和ASP.NET整合详细步骤 前置准备 框架搭建 数据填充 数据增/删/改 其他 介绍&使用场景 JqGrid不是一个 ...
- JS 原型的妙用
原型是JS的一个重要的特征,通过它可以实现类和实例直接的继承关系. 1.原型来来实现数据备份 // 通过原型来来实现数据备份 function p(x){ this.x = x; } p.protot ...
- UnknownHandler
未知处理器 从struts2.1 开始 ,struts2配置文件的DTD中增加了<unknown-handler-stack…/>和<unknown-handler-ref…/> ...
- 【转】 解读EOF
解读EOF 标签: fplinuxc语言filestream 2012-01-31 22:05 439人阅读 评论(0) 收藏 举报 分类: C.C++_程序设计(20) 我学习C语言的时候,遇到 ...
- 【06-23】js动画学习笔记01
<html> <head> <style> * { margin:0; padding:0; } #div1{ width:200px; height:200px; ...
- Bash 是如何从环境变量中导入函数的
在上文中曾说到: 所谓的环境变量的真实面目其实就是个任意字符串 Bash 在启动时会将 environ 数组中包含 = 号的字符串导入成为自己的变量 Bash 在启动外部命令时会将自己内部标记为环境变 ...
- [Java] Java反射
首先推荐三个十分有趣的网站: http://www.programcreek.com/simple-java/ http://tutorials.jenkov.com/ http://www.meet ...
- PHP保留2位小数 格式化小数、浮点数
JS保留两位小数例子 四舍五入使用函数 toFixed() <html> <head> </head> <script language="java ...
- 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...
- MySQL Cluster 集群简介
简介 MySQL集群是一种在无共享架构(SNA,Share Nothing Architecture)系统里应用内存数据库集群的技术.这种无共享的架构可以使得系统使用低廉的硬件获取高的可扩展性. My ...