[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. ...
随机推荐
- C5509A启动使用定时器
#include <stdio.h> #include <csl.h> #include <csl_pll.h> #include <csl_chip.h&g ...
- 如何在nlp问题中定义自己的数据集
我之前大致写了一篇在pytorch中如何自己定义数据集合,在这里如何自定义数据集 不过这个例子使用的是image,也就是图像.如果我们用到的是文本呢,处理的是NLP问题呢? 在解决这个问题的时候,我在 ...
- JS简写
本文来源于多年的 JavaScript 编码技术经验,适合所有正在使用 JavaScript 编程的开发人员阅读. 本文的目的在于帮助大家更加熟练的运用 JavaScript 语言来进行开发工作. 文 ...
- C语言指针篇(二)多级指针
多级指针 多级指针常常使用于数组.这里仅仅介绍一下它长什么样,后文会再次详细对比使用. 多级指针呢,常见的有二级指针.见图. 二级指针的 ...
- emplace_back
c++11 的 list deque 和 vector 增加了emplace_back函数,相对于push_back函数,它减少了一次类的构造,因此效率更高,推荐使用. #include <li ...
- linux处理僵尸进程
由来 在linux下,如果一个进程终止,内核会释放该进程使用的所有存储区,关闭所有文件句柄等,但是,内核会为每个终止子进程保留一定量的信息.这些信息至少包括进程ID,进程的终止状态,以及该进程使用的C ...
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- 2、spring boot 配置文件
配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoot自 ...
- 关于Python、Java、C#语言的一些比较
不能说某某语言不好! 首先,千万别说某一个语言好不好,应为这样的用词是错的,我曾经在好多场合听到一些程序员说java好,.net不好这类的话. 其实语言不分好坏,只是在具体的某些领域或业务场景上不合适 ...
- 机器学习tensorflow框架初试
本文来自网易云社区 作者:汪洋 前言 新手学习可以点击参考Google的教程.开始前,我们先在本地安装好 TensorFlow机器学习框架. 首先我们在本地window下安装好python环境,约定安 ...