Search for a Range
2015-08-14 14:10
232 查看
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
For example,
Given
return
class Solution {
public:
int helper(vector<int>& nums, int target)
{
int len = nums.size();
int left = 0;
int right = len - 1;
int pos = -1;
while(1)
{
if(nums[right] < target || nums[left] > target)
{
break;
}
if(nums[(left + right)/2] == target)
{
pos = (left + right)/2;
break;
}
if(nums[(left + right)/2] > target)
{
left = left;
right = (left + right)/2 - 1;
continue;
}
if(nums[(left + right)/2] < target)
{
left = (left + right)/2 + 1;
right = right;
continue;
}
}
return pos;
}
vector<int> searchRange(vector<int>& nums, int target)
{
int len = nums.size();
vector<int> vec;
if(len == 0)
{
vec.push_back(-1);
vec.push_back(-1);
return vec;
}
int pos = -1;
int left = 0;
int right = len - 1;
pos = helper(nums, target);
if(pos == -1)
{
vec.push_back(-1);
vec.push_back(-1);
return vec;
}
int start = pos;
int end = pos;
left = 0;
right = start;
//assert(0);
while(1)
{
if(nums[(left + right)/2] == target)
{
if((left + right)/2 == 0)
{
start = 0;
break;
}else
{
if(nums[(left + right)/2 - 1] < target)
{
start = (left + right)/2;
break;
}else
{
left = left;
right = (left + right)/2 - 1;
}
}
continue;
}
if(nums[(left + right)/2] < target)
{
left = (left + right)/2 + 1;
right = right;
continue;
}
}
//assert(0);
left = end;
right = len - 1;
//cout << start << endl;
while(1)
{
if(nums[(left + right)/2] == target)
{
if((left + right)/2 == len - 1)
{
end = len - 1;
break;
}else
{
if(nums[(left + right)/2 + 1] > target)
{
end = (left + right)/2;
break;
}else
{
left = (left + right)/2 + 1;
right = right;
}
}
continue;
}
if(nums[(left + right)/2] > target)
{
left = left;
right = (left + right)/2 - 1;
continue;
}
}
//assert(0);
//cout << end << endl;
vec.push_back(start);
vec.push_back(end);
return vec;
}
};
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].
class Solution {
public:
int helper(vector<int>& nums, int target)
{
int len = nums.size();
int left = 0;
int right = len - 1;
int pos = -1;
while(1)
{
if(nums[right] < target || nums[left] > target)
{
break;
}
if(nums[(left + right)/2] == target)
{
pos = (left + right)/2;
break;
}
if(nums[(left + right)/2] > target)
{
left = left;
right = (left + right)/2 - 1;
continue;
}
if(nums[(left + right)/2] < target)
{
left = (left + right)/2 + 1;
right = right;
continue;
}
}
return pos;
}
vector<int> searchRange(vector<int>& nums, int target)
{
int len = nums.size();
vector<int> vec;
if(len == 0)
{
vec.push_back(-1);
vec.push_back(-1);
return vec;
}
int pos = -1;
int left = 0;
int right = len - 1;
pos = helper(nums, target);
if(pos == -1)
{
vec.push_back(-1);
vec.push_back(-1);
return vec;
}
int start = pos;
int end = pos;
left = 0;
right = start;
//assert(0);
while(1)
{
if(nums[(left + right)/2] == target)
{
if((left + right)/2 == 0)
{
start = 0;
break;
}else
{
if(nums[(left + right)/2 - 1] < target)
{
start = (left + right)/2;
break;
}else
{
left = left;
right = (left + right)/2 - 1;
}
}
continue;
}
if(nums[(left + right)/2] < target)
{
left = (left + right)/2 + 1;
right = right;
continue;
}
}
//assert(0);
left = end;
right = len - 1;
//cout << start << endl;
while(1)
{
if(nums[(left + right)/2] == target)
{
if((left + right)/2 == len - 1)
{
end = len - 1;
break;
}else
{
if(nums[(left + right)/2 + 1] > target)
{
end = (left + right)/2;
break;
}else
{
left = (left + right)/2 + 1;
right = right;
}
}
continue;
}
if(nums[(left + right)/2] > target)
{
left = left;
right = (left + right)/2 - 1;
continue;
}
}
//assert(0);
//cout << end << endl;
vec.push_back(start);
vec.push_back(end);
return vec;
}
};
相关文章推荐
- Kurento应用开发指南(以Kurento 5.0为模板) 之二:示例教程helloworld
- Linux基础二(个人整理)
- 笔试面试知识点转载
- spring之多线程使用
- springMVC
- 黑马程序员--java技术blog---第十一篇:反射
- Opencv YAML和XML格式文件操作详解
- Objective-c中多线程使用AsyncSocket时报错
- Opencv YAML和XML格式文件操作详解
- hdu 3117
- RTSP
- HDU-5373-水题-卡常数时间
- git 使用
- IOS常用第三方框架 - Rosie
- Unix操作系统发展历史
- Android的线程(Thread)和服务(Service)的区别
- Android学习笔记之二:矩阵ColorMatrix实现图片ARGB变化
- Flume结合Spark测试
- python2.7搭建
- 查看path CLASSPATH命令