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 ...
随机推荐
- 微信小程序将view动态填满全屏
一.在app.js利用官方方法获取设备信息,将获取到的screenHeight.windowHeight度量单位统一由rpx换算为px 注:官方文档给出 [rpx换算px (屏幕宽度/750) ][ ...
- ubuntu启动脚本
下午分析了一下mysql的启动脚本,找到这篇,记录一下,目前很多服务都是以这种方式封装,后面自己写来借鉴一下 http://blog.fens.me/linux-upstart/
- 解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误
解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误 本人在使用pl/sql developer 客户端调用 ...
- [技巧篇]14.据说SSH框架需要的监听器,IntrospectorCleanupListener
开发这么久,我也没有使用过IntrospectorCleanupListener监听器,今天偶尔看到一篇文章,虽然没有怎么读懂,也不太理解,但是好像给官方提供一些解释!给自己留一个备注,多点东西因为没 ...
- ZooKeeper配额指南(十)
配额 ZK有命名空间和字节配额.你可以使用ZooKeeperMain类来设置配额.ZK打印警告信息如果用户超过分配给他们的配额.这些信息被打印到ZK的日志中. $java -cp zookeeper. ...
- js的作用域深入理解
一.什么是作用域 作用域是指对某一变量和方法具有访问权限的代码空间,Javascript的作用域只有两种:全局作用域和本地作用域,本地作用域是按照函数来区分的(即全局变量和局部变量)) 局部变量:只有 ...
- Jmeter-Java heap内存溢出
使用jmeter进行压力测试时遇到一段时间后报内存溢出outfmenmory错误,导致jmeter卡死了,先尝试在jmeter.bat中增加了JVM_ARGS="-Xmx2048m -Xms ...
- 图连通性【tarjan点双连通分量、边双联通分量】【无向图】
根据 李煜东大牛:图连通性若干拓展问题探讨 ppt学习. 有割点不一定有割边,有割边不一定有割点. 理解low[u]的定义很重要. 1.无向图求割点.点双联通分量: 如果对一条边(x,y),如果low ...
- GitLab 迁移与升级
参考: [ 博客园 BigBao ] 环境说明: OS: CentOS 7.x gitlab-ce 初始版本: 8.8.5 gitlab-ce 升级到版本: 11.2.3 升级方式: rpm 安装升级 ...
- Berland National Library
题目链接:http://codeforces.com/problemset/problem/567/B 题目描述: Berland National Library has recently been ...