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 ...
随机推荐
- Promise用法总结
1. Promise的状态 Promise对象有三个状态: 1. 进行中(pending) 2. 成功(resolved) 3. 失败(rejected) 2. 生成一个Promise对象 ...
- Semphore信号量的使用
前言:在多线程环境的同步中,我们为了让每个线程具有同步的作用,经常采用synchronize.reetrantlock等同步手段进行上锁,以便在同一时间只能有一个线程具有访问变量和读写变量的权力.然而 ...
- PhoneGap API介绍:Camera
本文将介绍PhoneGap API——Camera:使用设备的摄像头采集照片,对象提供对设备默认摄像头应用程序的访问. 方法: camera.getPicture 参数: cameraSuccess ...
- 人工智能----TensorFlow开篇简介
Artificial Intelligence,也就是人工智能.TensorFlow是Google在2015年底开源的项目 TensorFlow的论文地址:http://download.tensor ...
- AndroidStudio下加入百度地图的使用(一)——环境搭建
AndroidStudio下加入百度地图的使用(一)--环境搭建 最近有学生要做毕业设计,会使用到定位及地图信息的功能,特此研究了一下,供大家参考,百度定位SDK已经更新到了5.0,地图SDK已经更新 ...
- 【Android】完善Android学习(二:API 2.3.4)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...
- 【转】js JavaScript 的性能优化:加载和执行
JavaScript 的性能优化:加载和执行 转自:https://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/ 随着 Web2.0 技术的 ...
- 【51NOD-0】1049 最大子段和
[算法]DP [题解]开long long…… #include<cstdio> #include<algorithm> #include<cstring> usi ...
- DotNETCore 学习笔记 WebApi
API Description Request body Response body GET /api/todo Get all to-do items None Array of to-do ite ...
- MSSQL 错误:在将 varchar 值 '1,2,3,5,6' 转换成数据类型 int 时失败。
MSSQL 错误:在将 varchar 值 '1,2,3,5,6' 转换成数据类型 int 时失败.