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 ...
随机推荐
- Vue.js中的常用的指令缩写
Vue.js为两个最为常用的指令提供了特别的缩写: v-bind缩写 <!--完整语法--> <a v-bind:href="url">测试</a&g ...
- noip模拟赛 保留道路
[问题描述] 很久很久以前有一个国家,这个国家有N个城市,城市由1,2,3,…,N标号,城市间有M条双向道路,每条道路都有两个属性g和s,两个城市间可能有多条道路,并且可能存在将某一城市与其自身连接起 ...
- rand、randi和randn的区别?
1,rand 生成均匀分布的伪随机数.分布在(0~1)之间 主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数 rand(m,n,'double')生成指定精度的均匀分布的伪随机数,参数还可 ...
- c#知识梳理
转:http://www.cnblogs.com/zhouzhou-aspnet/articles/2591596.html 本文是一个菜鸟所写,本文面向的人群就是像我这样的小菜鸟,工作一年也辛辛苦苦 ...
- 东方14ACM小组 Challenge 11
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...
- PowerDesigner16连接mysql5.6逆向生成PDM
一:首先安装ODBC驱动 https://dev.mysql.com/downloads/connector/odbc/ ,安装32位驱动 二:然后配置好ODBC数据源,控制面板\系统和安全\管理 ...
- Jmeter-Java heap内存溢出
使用jmeter进行压力测试时遇到一段时间后报内存溢出outfmenmory错误,导致jmeter卡死了,先尝试在jmeter.bat中增加了JVM_ARGS="-Xmx2048m -Xms ...
- 【51NOD-0】1089 最长回文子串 V2(Manacher算法)
[算法]回文树 #include<cstdio> #include<algorithm> #include<cstring> using namespace std ...
- CSS 竖线 点 时间节点
效果如图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- 设计模式之Factory
设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) 介绍: Factory Pattern有3种当然是全部是creational pattern. 1.S ...