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 ...
随机推荐
- Linux之同步互斥阻塞20160703
主要介绍一下Linux下的互斥与阻塞方面的知识: 1. 原子操作 原子操作指的是在执行过程中不会被别的代码路径所中断的操作. 常用原子操作函数举例: atomic_t v = ATOMIC_INIT( ...
- mybaties实体的 Mapper.xml文件中自定义sql时模糊查询的写法
<select id=selectByNameLike" parameterType="string" resultMap="BaseResultMap ...
- UIView的autoresizingMask属性研究
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum ...
- [转载][mysql]mysql字符集干货
源地址:http://www.blogjava.net/zyskm/archive/2013/04/09/361888.html 字符集的概念大家都清楚,校对规则很多人不了解,一般数据库开发中也用不到 ...
- 单线程的redis为什么达到每秒万级的处理速度?
纯内存访问,redis将所有数据都放在内存中,内存响应时间大约为100纳秒,这是redis达到每秒万级级别访问的重要基础. 非阻塞IO,redis使用epoll作为IO多路复用技术的实现,再加上red ...
- 自己实现KNN算法
import numpy as np from math import sqrt from collections import Counter class KNNClassifier(object) ...
- 【Foreign】K优解 [堆]
K优解 Time Limit: 20 Sec Memory Limit: 512 MB Description 给定n个行数,每行m个.在每行中选出一个数来,求出前 k 小的异或和. Input 第 ...
- quick-cocos2d-x数据存储 UserDefault GameState io
看了quick-cocos2d-x 的framework,发现里面有一个GameState,查了下,是数据存储的类,于是稍稍总结下我用到过的数据存储方式吧. 一共是三种方法: cc.UserDefau ...
- 大聊Python----协程
协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...
- 首行缩进css
html首行缩进2字符,可以使用CSS属性中的[text-indent]进行设置. 设置代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...