数据结构设计类题目,参考网上的代码:

/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* public:
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* bool isInteger() const;
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* int getInteger() const;
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* const vector<NestedInteger> &getList() const;
* };
*/
class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {
begins.push(nestedList.begin());
ends.push(nestedList.end());
} int next() {
return (begins.top()++)->getInteger();
} bool hasNext() {
while(begins.size())
{
if(begins.top() == ends.top())
{
begins.pop();
ends.pop();
}
else
{
auto val = begins.top();
if(val->isInteger())
return true;
begins.top()++;
begins.push(val->getList().begin());
ends.push(val->getList().end());
}
}
return false; } private:
stack<vector<NestedInteger>::iterator> begins, ends;
}; /**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i(nestedList);
* while (i.hasNext()) cout << i.next();
*/

leetcode341的更多相关文章

  1. [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

随机推荐

  1. scrollTop兼容处理

    使用jQuery2.0以下版本的scrollTop()函数来设置当然兼容性当然很好,但有时需要为滚动设置滑动效果.比如,使用animate函数,这里需要做些兼容性处理: 实例:http://sandb ...

  2. js去重复和取重复数据

    js数组中取重复数据的方法: 方法一:去重复数据 <script> Array.prototype.distinct=function(){ var a=[],b=[]; for(var ...

  3. 推荐近乎免费的调试神器——OzCode

    当一只断点打在 Visual Studio 的代码编辑器中,程序命中断点的那一刻,调试才刚刚开始……这个时候忙碌的手在键盘和鼠标之间来回跳跃,试图抓住每一次单步执行带来的状态改变. 如果命中断点的那一 ...

  4. hadoop入门手册3:Hadoop【2.7.1】初级入门之命令指南

    问题导读1.hadoop daemonlog管理员命令的作用是什么?2.hadoop如何运行一个类,如何运行一个jar包?3.hadoop archive的作用是什么? 概述 hadoop命令被bin ...

  5. 《DSP using MATLAB》示例 Example 9.4

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  6. RabbitMQ介绍及windows下安装使用

    RebbitMQ介绍 RabbitMQ是一个由 Erlang (一种通用的面向并发的编程语言)开发的AMQP(Advanced Message Queue )的开源实现,Rabbit MQ 是建立在E ...

  7. 关于fpga优化的set input delay 和 set output delay

    set input delay 和set output delay 首先必须明确的是指的外部delay,而非input或output的内部delay,那么这外部delay包含什么呢?包含1,外部路径延 ...

  8. Linux环境下Maven的.m2文件夹

    aven中的.m2文件夹 安装完maven是没有.m2文件夹的.在linux中以.开头的文件夹都是隐藏的.当使用maven命令的时候,maven自动会创建.m2文件夹. 运行命令mvn help:sy ...

  9. sql的一些事件处理

    select getdate() select Convert(varchar(10),getdate(),120) yyyy-mm-ddselect Convert(varchar(20),getd ...

  10. 初学HTML之HTML介绍

    众所周知现在的H5.大数据.云计算都是热门的.其实想学好一门语言重点是多看多想多写多练. 我在博客中会从基础开始讲解HTML4.0.中间加入HTML5的新标签 在这先给大家推荐几个开发工具: note ...