[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. ...
随机推荐
- SpringMVC文件上传——bean的配置【org.springframework.web.multipart.commons.CommonsMultipartResolver】
一.简介 Spring MVC支持一个通用的多路上传解析器CommonsMultipartResolver,在Spring的配置文件中对CommonsMultipartResolver Bean进行配 ...
- spark提交运算原理
前面几天元旦过high了,博客也停了一两天,哈哈,今天我们重新开始,今天我们介绍的是spark的原理 首先先说一个小贴士: spark中,对于var count = 0,如果想使count自增,我们不 ...
- 大话CNN经典模型:LeNet
近几年来,卷积神经网络(Convolutional Neural Networks,简称CNN)在图像识别中取得了非常成功的应用,成为深度学习的一大亮点.CNN发展至今,已经有很多变种,其中有 ...
- python面向对象(进阶篇)
本篇将详细介绍Python 类的成员.成员修饰符.类的特殊成员. 类的成员: 类的成员可以分为三大类:字段(变量).方法.属性. 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对 ...
- HDFS写数据和读数据流程
HDFS数据存储 HDFS client上传数据到HDFS时,首先,在本地缓存数据,当数据达到一个block大小时.请求NameNode分配一个block. NameNode会把block所在的Dat ...
- 10.bootstrap分页,点击哪个分页号,哪个分页号就active
1.分页,点击哪个分页号,哪个分页号就active <nav> <ul class="pagination"> <li><a href=& ...
- CC3200在AP模式的TCP sock作为客户端连接时返回SL_ECONNREFUSED(-111) Connection refused
1. CC3200处于AP模式(电脑无线连接CC3200的WIFI信号),开启一个TCP socket,这个socket作为TCP客户端去连接TCP服务器端 struct sockaddr_in ad ...
- Jsoncpp 编译
1. linux下编译jsoncpp 从(http://jsoncpp.sourceforge.net/)下载源码包“jsoncpp-src-0.5.0.tar.gz”,解压后在其解压后目录中运行 $ ...
- equals和toString
Object的equals方法默认比较地址值.所以当需要比较两个对象的内容时需要重写equals方法.
- 使用系统的某些block api(如UIView的block版本写动画时),是否也考虑循环引用问题?
系统的某些block api中,UIView的block版本写动画时不需要考虑,但也有一些api 需要考虑 以下这些使用方式不会引起循环引用的问题 [UIView animateWithDuratio ...