// 面试题59(二):队列的最大值
// 题目:给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值。例如,
// 如果输入数组{2, 3, 4, 2, 6, 2, 5, 1}及滑动窗口的大小3,那么一共存在6个
// 滑动窗口,它们的最大值分别为{4, 4, 6, 6, 6, 5}, #include <iostream>
#include <deque>
#include <exception> using namespace std; template<typename T> class QueueWithMax
{
public:
QueueWithMax() : currentIndex()
{
} void push_back(T number)
{
while (!maximums.empty() && number >= maximums.back().number)//和上一题一样,尾节点不能大于当前节点,否则删除
maximums.pop_back(); InternalData internalData = { number, currentIndex };
data.push_back(internalData);//data用来保存原数据
maximums.push_back(internalData);//maximums用来保存最大值序列 ++currentIndex;
} void pop_front()
{
if (maximums.empty())
throw new exception("queue is empty"); if (maximums.front().index == data.front().index)//弹出头结点的时候,要检查弹出的是否是最大值节点
maximums.pop_front(); data.pop_front();
} T max() const
{
if (maximums.empty())
throw new exception("queue is empty"); return maximums.front().number;//队列最大值是maximums头结点
} private:
struct InternalData
{
T number;
int index;
}; deque<InternalData> data;
deque<InternalData> maximums;
int currentIndex;
}; // ====================测试代码====================
void Test(const char* testName, const QueueWithMax<int>& queue, int expected)
{
if (testName != nullptr)
printf("%s begins: ", testName); if (queue.max() == expected)
printf("Passed.\n");
else
printf("FAILED.\n");
} int main(int argc, char* argv[])
{
QueueWithMax<int> queue;
// {2}
queue.push_back();
Test("Test1", queue, ); // {2, 3}
queue.push_back();
Test("Test2", queue, ); // {2, 3, 4}
queue.push_back();
Test("Test3", queue, ); // {2, 3, 4, 2}
queue.push_back();
Test("Test4", queue, ); // {3, 4, 2}
queue.pop_front();
Test("Test5", queue, ); // {4, 2}
queue.pop_front();
Test("Test6", queue, ); // {2}
queue.pop_front();
Test("Test7", queue, ); // {2, 6}
queue.push_back();
Test("Test8", queue, ); // {2, 6, 2}
queue.push_back();
Test("Test9", queue, ); // {2, 6, 2, 5}
queue.push_back();
Test("Test9", queue, ); // {6, 2, 5}
queue.pop_front();
Test("Test10", queue, ); // {2, 5}
queue.pop_front();
Test("Test11", queue, ); // {5}
queue.pop_front();
Test("Test12", queue, ); // {5, 1}
queue.push_back();
Test("Test13", queue, ); system("pause");
return ;
}

《剑指offer》第五十九题(队列的最大值)的更多相关文章

  1. 《剑指offer》第十九题(正则表达式匹配)

    // 面试题19:正则表达式匹配 // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符'.' // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题 ...

  2. 《剑指offer》第二十九题(顺时针打印矩阵)

    // 面试题29:顺时针打印矩阵 // 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. #include <iostream> void PrintMatrixInC ...

  3. 剑指Offer(二十九):最小的K个数

    剑指Offer(二十九):最小的K个数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baid ...

  4. 《剑指offer》第二十六题(树的子结构)

    // 面试题26:树的子结构 // 题目:输入两棵二叉树A和B,判断B是不是A的子结构. #include <iostream> struct BinaryTreeNode { doubl ...

  5. 《剑指offer》第二十八题(对称的二叉树)

    // 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...

  6. 《剑指offer》第二十四题(反转链表)

    // 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...

  7. 《剑指offer》第二十二题(链表中倒数第k个结点)

    // 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...

  8. 《剑指offer》第十八题(删除链表中重复的结点)

    // 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...

  9. 《剑指offer》第十八题(在O(1)时间删除链表结点)

    // 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点. #include <iostream> ...

  10. 《剑指offer》第十六题(数值的整数次方)

    // 面试题:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需要考 ...

随机推荐

  1. css盒子模型、边框border、外边距margin、填充padding、轮廓outline

    盒子模型:盒子默认的宽度为容器的宽度,也可以自省设定宽度,高度根据内容适应,也可以自行设定高度.min-height设定最小高度 一个盒子包括外边距.边框.内边距和实际内容 Margin(外边距):清 ...

  2. JSch : channel never closed or EOF 通道未关闭

    最近,我们的项目在开发远程节点管理的时候,使用了jsch库.在测试的时候发现有个节点在cmd执行完成之后,channel.isClosed()一直都是false,导致请求无法返回,但是其它有些节点就没 ...

  3. 问题:oracle 12c rac数据库服务器的home目录丢失问题解决2018-06-25 18:30

    问题原因:是由于运维粗心,在缩容/home(此目录下挂载了逻辑卷lv_home)时没有先缩小文件系统(resize2fs)也没有备份,导致home数据损坏,重启时系统无法正常启动 解决方案:跳过此ho ...

  4. 线程同步——用户模式下线程同步——Interlocked实现线程同步

    线程同步分为用户模式下的线程同步和内核对象的线程同步. 当然用户模式下的线程同步实现速度比内核模式下快,但是功能也有局 //1.利用原子访问: Interlocked系列函数,关于Interlocke ...

  5. FireMonkey 源码学习(6)

    (6)GetGlyph和GetBaseline TFontGlyphManager是一个抽象类,在不同平台上的实现是不同的,以Windows为例,在FMX.FontGlyphs.Win.pas文件中定 ...

  6. Bootstrap3基础 input-group glyphicon 输入框组与glyphicon图标

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  7. codevs1048石子归并

    codevs1048 石子归并  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 传送门  http://codevs.cn/problem/1048/ 题目描述 ...

  8. Jenkins serving Cake: our recipe for Windows

    https://novemberfive.co/blog/windows-jenkins-cake-tutorial/ Where we started, or: why Cake took the ...

  9. 在Jenkins中集成Sonarqube

    Analyzing with SonarQube Scanner for MSBuild Global Configuration This step is mandatory if you want ...

  10. Android Studio 快捷键、Debug的使用

    https://blog.csdn.net/q908555281/article/details/49331371 1.快捷键      个人习惯常用快捷键      在Eclipse中常用的快捷键 ...