[LeetCode] 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].
#include <vector>
#include <iostream>
using namespace std; class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int > ret(,-);
if(n<) return ret;
int mid = helpFun(A,,n-,target);
if(mid !=-){
ret[]=helpLeft(A,,mid,target);
ret[]=helpRight(A,mid,n-,target);
}
return ret;
} int helpLeft(int *a,int lft, int rgt, int & tar)
{
if(a[lft]==tar) return lft;
if(lft+==rgt) return rgt;
int mid = (lft+rgt)/;
if(a[mid]==tar) return helpLeft(a,lft,mid,tar);
else return helpLeft(a,mid,rgt,tar);
} int helpRight(int *a,int lft, int rgt, int & tar)
{
if(a[rgt]==tar) return rgt;
if(lft+==rgt) return lft;
int mid = (lft+rgt)/;
if(a[mid]==tar) return helpRight(a,mid,rgt,tar);
else return helpRight(a,lft,mid,tar);
} int helpFun(int *a,int lft, int rgt, int & tar)
{
if(lft>rgt) return -;
if(lft == rgt){
if(tar==a[lft]) return lft;
return -;
}
if(lft + == rgt){
if(a[lft]==tar) return lft;
if(a[rgt]==tar) return rgt;
return -;
}
int mid = (lft+rgt)/;
if(a[mid]==tar) return mid;
if(a[mid]<tar) return helpFun(a,mid+,rgt,tar);
else return helpFun(a,lft,mid-,tar);
}
}; int main()
{
int A[]={, , , , , };
Solution sol;
vector<int > ret = sol.searchRange(A,sizeof(A)/sizeof(int),);
cout<<ret[]<<ret[]<<endl;
return ;
}
附件是一份集成的比较好的,思路是一样:
vector<int> searchRange(int a[], int n, int target) {
vector<int> result(,-);
int left = INT_MAX;
int right = INT_MIN;
binSearch(a, , n-, n, target, left, right);
if (left != INT_MAX && right != INT_MIN) {
result[] = left;
result[] = right;
}
return result;
}
void binSearch(int a[], int l ,int h, int n, int target, int& left, int& right) {
if (l > h) return;
int m = (l+h)/;
if (a[m] > target) {
binSearch(a,l,m-,n,target, left, right);
} else if (a[m] < target) {
binSearch(a,m+,h,n,target, left, right);
} else {
if (m < left) {
left = m;
if (m > && a[m-] == target) {
binSearch(a,l,m-,n,target,left,m);
}
}
if (m > right) {
right = m;
if (m < n- && a[m+] == target) {
binSearch(a,m+,h,n,target,m,right);
}
}
}
}
[LeetCode] Search for a Range 二分搜索的更多相关文章
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [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. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- [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 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. You ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- 2019-04-11 python入门学习——配置机器及搭建开发环境
# 在windows操作系统中搭建python 3.x版本的开发环境,开发工具为 Anaconda 3. # 1.1 下载及安装Anaconda 3 Anaconda的特点:集成性高,包含很多常用的开 ...
- python爬虫:利用BeautifulSoup爬取链家深圳二手房首页的详细信息
1.问题描述: 爬取链家深圳二手房的详细信息,并将爬取的数据存储到Excel表 2.思路分析: 发送请求--获取数据--解析数据--存储数据 1.目标网址:https://sz.lianjia.com ...
- java8之list集合中取出某一属性的方法
上代码 List<User> list = new ArrayList<User>(); User user1 = new User("第一位"," ...
- [Bzoj1034][ZJOI2008]泡泡堂BNB(贪心)
Description 题目链接 Solution 这题就是一个贪心, 如果最弱的能赢对方最弱的就赢 否则最强的能赢对面最强的就赢 否则最弱的换对面最强 Code #include <cstdi ...
- 17-比赛1 F - 较小元素 Weak in the Middle (set)
Seg-El has last chance to make the final changes in order to prevent the destruction of Krypton. He ...
- dfs序线段树
dfs序+线段树,啥?如果在一棵树上,需要你修改一些节点和查询一些节点,如果直接dfs搜的话肯定超时,那用线段树?树结构不是区间啊,怎么用?用dfs序将树结构转化为一个区间,就能用线段树进行维护了. ...
- python基础之函数参数、嵌套、返回值、对象、命名空间和作用域
函数的使用原则 函数的使用必须遵循:先定义后使用的原则 函数的定义,与变量的定义是相似的,如果没有事先定义函数而直接引用就相当于在引用一个不存在变量名 定义阶段:只检测语法,不执行代码,当出现语法错误 ...
- js调用js的方法
a.js文件调用b.js文件时,需要在a.js的第一行添加代码 document.write(" <script language=\"javascript\" s ...
- Android ANR详解
如何避免KeyDispatchTimeout 1:UI线程尽量只做跟UI相关的工作 2:耗时的工作(比如数据库操作,I/O,连接网络或者别的有可能阻碍UI线程的操作)把它放入单独的线程处理 3:尽量用 ...
- echo shell commands as they are executed
http://stackoverflow.com/questions/2853803/in-a-shell-script-echo-shell-commands-as-they-are-execute ...