[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. ...
随机推荐
- P1219 N皇后
P1219 N皇后 题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序 ...
- Android面试收集录16 Android动画总结
一.Android 动画分类 总的来说,Android动画可以分为两类,最初的传统动画和Android3.0 之后出现的属性动画: 传统动画又包括 帧动画(Frame Animation)和补间动画( ...
- 局域网&广域网&Internet&计算机通信过程
1.局域网 覆盖范围小(100m以内),自己花钱购买,自己单位来维护,带宽固定的(10M,100M,1000M) 2.Internet ISP,有自己的机房,对网民提供访问Internet连接 3.广 ...
- 直接插入排序&希尔排序
1.直接插入排序 时间复杂度O(n2) 工作原理: 通过构建有序序列,对于未排序数据,在已排序的序列中,从后向前扫描,找到相应的位置并插入. 插入排序在实现上,在从后向前扫描的过程中,需要反复把已排序 ...
- svn Previous operation has not finished; run 'cleanup' if it was interrupted
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...
- 安装macports
Mac下面除了用dmg.pkg来安装软件外,比较方便的还有用MacPorts来帮助你安装其他应用程序,跟BSD中的ports道理一样.MacPorts就像apt-get.yum一样,可以快速安装些软件 ...
- android 文件保存
将数据保存在外部存储器上 /* Checks if external storage is available for read and write */ public boolean isExter ...
- 阿里云ECS Ubuntu安装PHP+Mysql+Apache+Nginx+Redis+Discuz
http://www.linuxdiyf.com/linux/13662.html http://blog.csdn.net/wangnan537/article/details/47868659 h ...
- [译]9-spring bean的生命周期
spring中bean的生命周期比较容易理解.bean在实例化之后有时需要调用某个初始化方法进行一些初始化的工作.同样的 ,当bean在销毁之前有时需要做一些资源回收的工作. 尽管bean在实例化和销 ...
- JavaScript里面的正则以及eval
1.eval JavaScript中的eval是Python中eval和exec的合集,既可以编译代码也可以获取返回值. eval() EvalError 执行字符串中的JavaScript代码 ...