【Search for a Range】cpp
题目:
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].
代码:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int pos = Solution::findPos(nums, target, , nums.size()-);
if ( pos==- )
{
ret.push_back(-);
ret.push_back(-);
return ret;
}
int l = Solution::findLeft(nums, target, , pos);
ret.push_back(l);
int r = Solution::findRight(nums, target, pos+, nums.size()-);
ret.push_back(r);
return ret;
}
static int findPos(vector<int>& nums, int target, int begin, int end)
{
if ( begin>end ) return -;
int mid = (begin+end)/;
if ( nums[mid]==target ) return mid;
if ( nums[mid]>target )
{
return Solution::findPos(nums, target,begin,mid-);
}
else
{
return Solution::findPos(nums, target, mid+, end);
}
}
static int findLeft(vector<int>& nums, int target, int begin, int end)
{
if ( begin>end ) return begin;
int mid = (begin+end)/;
if ( nums[mid]<target )
{
return Solution::findLeft(nums, target, mid+, end);
}
else
{
return Solution::findLeft(nums, target, begin, mid-);
}
}
static int findRight(vector<int>& nums, int target, int begin, int end)
{
if ( begin>end ) return end;
int mid = (begin+end)/;
if ( nums[mid]>target )
{
return Solution::findRight(nums, target, begin, mid-);
}
else
{
return Solution::findRight(nums, target, mid+, end);
}
}
};
tips:
按照常规的思路写的。
1. 首先二分查找target变量的某个位置,如果没有则直接返回-1
2. 确定有target变量了,则分左右两边找
1)左侧:找最左边的target的元素
2)右侧:找最右边的target元素
注意处理好边界的case。
=================================
学习了一种STL的写法 代码如下:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
const int l = std::distance(nums.begin(), std::lower_bound(nums.begin(), nums.end(), target));
const int u = std::distance(nums.begin(), std::upper_bound(nums.begin(), nums.end(), target));if (nums[l]!=target)
{
ret.push_back(-);
ret.push_back(-);
}
else
{
ret.push_back(l);
ret.push_back(u>?u-:);
}
return ret;
}
};
非常简洁。
不知道为什么,在mac的sublime上coding时,prev() next() 这俩函数一直不让用。
=============================================
第二次过这道题,就是用二分查找的思路。找左边界,右边界。代码比第一次过的时候简洁一些。
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int l = -;
int r = -;
int begin = ;
int end = nums.size()-;
// search for left bound
while ( begin<=end )
{
int mid = (begin+end)/;
if ( nums[mid]==target )
{
l = mid;
end = mid-;
}
else if ( nums[mid]>target )
{
end = mid-;
}
else
{
begin = mid+;
}
}
if ( l==- ) { ret.push_back(l); ret.push_back(r); return ret; }
// search for right bound
begin = l;
end = nums.size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( nums[mid]==target )
{
r = mid;
begin = mid+;
}
else
{
end = mid-;
}
}
ret.push_back(l);
ret.push_back(r);
return ret;
}
};
【Search for a Range】cpp的更多相关文章
- leetcode 【 Search for a Range 】python 实现
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 【Search a 2D Matrix】cpp
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- 【String to Integer (atoi) 】cpp
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Hdu 4738【求无向图的桥】.cpp
题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...
- 【Merge K Sorted Lists】cpp
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 【Merge Two Sorted Lists】cpp
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 【Largest Rectangle in Histogram】cpp
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- 【Binary Tree Inorder Traversal】cpp
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- 【Binary Tree Preorder Traversal】cpp
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
随机推荐
- 《你是我的小羊驼》游戏ios源码
<ignore_js_op> <ignore_js_op> <ignore_js_op> <ignore_js_op>源码下载:http://code. ...
- ado.net工厂模式DbProviderFactories
DbProviderFactory f = DbProviderFactories.GetFactory(System.Configuration.ConfigurationManager.Conne ...
- POJ C++程序设计 编程题#1 编程作业—运算符重载
编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的MyIn ...
- C#中判断字符串中包含某个字符
C#判断字符串是否存在某个字符,如果存在进行替换. //定义一个字符串 string str=".net/Java/asp.net"; //检验“/” if(str.Cont ...
- HttpClient Post Form data and get Response String
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http:/ ...
- sqoop导入数据到hive---2
1.hive-table 从mysql导入数据到hive表中,可以使用--hive-table来指定hive的表名,不指定hive表名,则hive表名与mysql表名保持一致. sqoop impor ...
- php static延迟静态绑定
如果你是一个懒惰的程序员,你看到以下代码可能会恼火 abstract class U{ } class u1 extends U{ public static function create(){ r ...
- Mongodb 3.0 创建用户
MongoDB 3.0 安全权限访问控制,在添加用户上面3.0版本和之前的版本有很大的区别,这里就说明下3.0的添加用户的方法. 创建第一个用户(该用户需要有grant权限,即:账号管理的授权权限) ...
- [terry笔记]Oracle会话追踪(二):TKPROF
接上一笔记[terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046 http://www.cnblogs.com/kkterry/p/3279282.html ...
- 10 款提高开发效率的 jQuery/CSS3 组件
前端开发是一项十分繁琐而又耗体力的工作,如何更有效率的开发我们的应用,很多人会选择适当地使用一些jQuery插件.今天就要给大家分享10款可以提高开发效率的jQuery/CSS3组件.部分插件可以下载 ...