【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 ...
随机推荐
- C#处理Excel
C#处理Excel C#处理Excel 前言 OleDb 具体操作 NPOI 具体操作 Excel C# NPOI OleDb 前言 最近需要对Excel进行加密解密操作,本身是一个简单的事情,通过 ...
- bootstrap中弹出窗体dialog的自定义
感谢nakupanda的https://github.com/nakupanda/bootstrap3-dialog 根据需要弹出窗体,但是可以移动,不遮挡下面的内容,所以就修改了源代码,添加了一个属 ...
- SQLServer附加数据库5120错误
装有MSSQL的电脑 需要附加的数据库文件(*.mdf)及其日志文件(*.ldf) 1. 打开SQL Server Management Studio,并连接上数据库.右键"数据库" ...
- [原创] 初识Agile/CMMI/Scrum
一.背景介绍 在朋友(aehyok)的建议下,初步去了解Visual Studio Online,简称VS Online(即原来的 Team Foundation Service,简称TFS) VS ...
- SQL中查看数据库各表的大小
SQL中查看数据库各表的大小 编写人:CC阿爸 2014-6-17 在日常SQL数据库的操作中,如何快速的查询数据库中各表中数据的大小. 以下有两种方法供参考: 第一种: create table # ...
- Zookeeper 脑裂
转自 http://blog.csdn.net/u010185262/article/details/49910301 Zookeeper zookeeper是一个分布式应用程序的协调服务.它是一个为 ...
- 一款点击图片进行无限循环的jquery手风琴特效
一款点击图片进行无限循环的jquery手风琴特效,点击手风琴折合点,可以无限循环的点击下去,很炫酷的手风琴哟! 还有每张图片的文字介绍,因为兼容IE6所以找来分享给大家这个jquery特效. 适用浏览 ...
- Ueditor防止代码自动清除
Ueditor功能真的很牛逼,可也有让人悲催的地方,尤其是自动清除代码,会将你默认的div标签改成p,挺让人闹心的,不过Ueditor的开发人员还是满热心的,搜遍网上无答案的时候,问了下他们,解决了 ...
- caused by android.system.errnoexception open failed eacces (permission denied)解决方案,安卓6.0(API23)权限问题
在API23+以上,不止要在AndroidManifest.xml里面添加权限 <uses-permission android:name="android.permission.RE ...
- JqueryUI
http://jqueryui.com/ http://www.runoob.com/jqueryui/jqueryui-tutorial.html