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. ...
随机推荐
- Python学习笔记——条件和循环
1.条件表达式 >>> x = 3 >>> x = 1 if x<3 else 2 >>> x 2 2.for语句用于序列类型 <1& ...
- Java Web学习笔记8
上下文参数(context-param) 由于init-param是配置在<servlet>标签里的,只能有这个Servlet来读取,因此它不是全局的参数,不能被其他的Servlet读取. ...
- struts2的DevMode模式
在实际应用开发或者是产品部署的时候,对应着两种模式: 1.开发模式(devMode),此时,DevMode=true 2.产品模式(proMode),此时,DevMode=false 在一些服务器或者 ...
- redirect()重新定向·
- 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?
file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...
- C和指针 第十一章 动态内存分配
声明数组时,必须指定数组长度,才可以编译,但是如果需要在运行时,指定数组的长度的话,那么就需要动态的分配内存. C函数库stdlib.h提供了两个函数,malloc和free,分别用于执行动态内存分配 ...
- http协议相关-待续
// 关于http的东西 function httpAction() { // http://localhost/blog/testurl.php?id=5 到目前为止 // 获取当前域名 // 获取 ...
- js正则
JS正则 test:判断字符串是否符合规定的正则 rep = /\d+/; rep.test("asdfoiklfasdf89asdfasdf") # true rep = /^\ ...
- sql之left join、right join、inner join的区别
sql之left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括 ...
- 解决java.lang.InstantiationError: sun.net.ftp.FtpClient
换用jdk1.6 .如果是放容器里的,也把容器的jdk换成1.6