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. ...
随机推荐
- 10月28日上午 PHP数据访问
1.建一个连接(连接PHP和MYSQL) $db = new MySQLi("localhost","root","666","t ...
- VirtualBox Guest Additions 在CentOS中无法安装的解决方法
安装时出现一步错误查看log为(log文件是 /var/log/vboxadd-install.log): /tmp/vbox.0/Makefile.include.header:94: *** Er ...
- AFNetworking certificate AFNetworking 证书设置
+ (AFSecurityPolicy*)customSecurityPolicy { // /先导入证书 NSString *cerPath = [[NSBundle mainBundle] pat ...
- Pandas-数据整理
Pandas包对数据的常用整理功能,相当于数据预处理(不包括特征工程) 目录 丢弃值 drop() 缺失值处理 isnull() & notnull() dropna() fillna() 值 ...
- App-Pass the password
V1.0 初始版本 注册一个帐号却不想使用简单密码? Pass the Password! 输入任意字符串,如反写或截取网站域名,我们帮你生成高安全性密码. 记住规则,忘记密码 . 下一次依照你的规则 ...
- LYDSY模拟赛day2 Divisors
/* 注意分解质因数,如果i是,那么n/i也是,这样就可以解决分解质因数的时间问题了 当 k ≥ 1 时,只有这些数的约数才会对答案产生贡献. 求出 m 个数的所有不超过 n 的约数,去重后统计即可. ...
- PHP大数(浮点数)取余
一般我们进行取余运算第一个想到的就是用百分号%,但当除数是个很大的数值,超出了int范围时,这样取余就不准确了. php大数(浮点数)取余函数 /** * php大数取余 * * @param int ...
- MySQL5.0安装图解
打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行"setup.exe",出现如下界面: 按"Next"继续 选择安装类 ...
- selenium+testng+ant+jenkins 手记
会不会搭建测试平台是一般测试工程师和高级测试工程师分水岭 ----tobecrazy 我们项目有现成的测试平台,使用的是selenium grid+testng+ant+jenkins+VM 但是我平 ...
- Spring读写xml文件
一.如果只是读取 新建一个 xml 文件,需要满足Spring格式: <?xml version="1.0" encoding="UTF-8"?> ...