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].
网上看到的思路更好,网上说的是,先用二分法找到左端点,再用二分搜索找到右端点。问题即得到解决。
我的思路不太好,我是首先找到等于target的索引(即以前用烂了的二分查找),然后以此为中心向两边扩。
1:我的方法:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int len=nums.size();
int l=,r=len-;
int mid;
vector<int>res;
int flag=;
while(l<=r)
{
mid=l+(r-l)/;
if(nums[mid]<target)
l=mid+;
else if(nums[mid]>target)
r=mid-;
else
{
flag=;
break;
}
}
if(flag==)
{
res.push_back(-);
res.push_back(-);
}
else
{
l=mid;
r=mid;
while(l>=&&nums[l]==target)
{
if(nums[l]==target)
l--;
}
while(r<=len-&&nums[r]==target)
{
if(nums[r]==target)
r++;
}
res.push_back(l+);
res.push_back(r-);
}
return res;
}
};
2:网上看到直接二分查找左右端点的方法:
class Solution {
public:
int begin = -, end = -;
vector<int> searchRange(int A[], int n, int target) {
vector<int>ans;
find(A,,n-,target);
ans.push_back(begin);
ans.push_back(end);
return ans;
}
void find(int A[], int l, int r, int target){
if(l > r) return ;
int mid = (l+r) >> ;
if(A[mid] == target){
if(begin == - || begin > mid)
begin = mid;
end = max(mid, end);
find(A,l,mid-,target);
find(A,mid+,r,target);
}
else if(A[mid] < target)
find(A,mid+,r,target);
else
find(A,l,mid-,target);
}
};
3:直接用 C++ STL 的 lower_bound 和 upper_bound 偷懒。
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
int* lower = lower_bound(A, A + n, target);
int* upper = upper_bound(A, A + n, target);
if (*lower != target)
return vector<int> {-, -};
else
return vector<int>{lower - A, upper - A - };
}
};
Search for a Range——稍微升级版的二分查找的更多相关文章
- 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. ...
- leetcode:Search a 2D Matrix(数组,二分查找)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode 704. Binary Search (二分查找)
题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime: 0 ms, faster than 100 % Memory Usage ...
- LeetCode总结--二分查找篇
二分查找算法尽管简单,但面试中也比較常见.经经常使用来在有序的数列查找某个特定的位置.在LeetCode用到此算法的主要题目有: Search Insert Position Search for a ...
- 数组查找算法的C语言 实现-----线性查找和二分查找
线性查找 Linear Search 用户输入学生学号的成绩 二分查找 Binary Search 要求数据表是已经排好序的 程序存在小的瑕疵
- 二分查找问题(Java版)
二分查找问题(Java版) 1.一般实现 package search; /** * @author lei 2011-8-17 */ public class BinarySearch ...
- 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 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
随机推荐
- bzoj2761: [JLOI2011]不重复数字(hash)
题目大意:给出N个数,要求把其中重复的去掉,只保留第一次出现的数.例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4. ...
- 【简单算法】17.字符串转整数(atoi)
题目: 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为 ...
- BAT-Java必考面试题集
2018最新<BAT Java必考面试题集> 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象 ...
- tp查找某字段,排除某字段,不用一次写那么多
更多的情况下我们都是查询某些字段,但有些情况下面我们需要通过字段排除来更方便的查询字段,例如文章详细页,我们可能只需要排除status和update_time字段,这样就不需要写一堆的字段名称了(有些 ...
- 【hdu1255】线段树求矩形面积交
题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...
- 阿里云服务器部署笔记二(python3、Flask、uWSGI、Nginx)
从git上把项目拉到服务器,项目可以在服务器上运行后,就只需要配置uwsgi和nginx了.它们的逻辑关系是:外部请求->nginx->uwsgi->项目实例. 一.配置uwsgi ...
- SVG(可缩放矢量图形)
SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C("World Wide W ...
- <script>标签的属性
1.async 表示立即下载资源,但不妨碍下载其他资源或等待加载其他脚本,脚本相对于页面的其它部分异步执行,只对外部脚本文件有效 2.defer 表示脚本可以延迟到文档完全被解析和显示之后再执行,只对 ...
- ie8下input文字偏上select文字偏下
1.ie8下input文字偏上 正常情况下input的显示情况如下 当设置input的高度时,就会出现文字不垂直居中偏上的情况,如图 解决方案 强input的行高line-height与其高度设置一致 ...
- [转]python os模块 常用命令
python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...