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

/**
* // 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. Unity是什么?

    Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 1. 它提供了创建(或者装配)对象实例的机制,而这些对象实例可能还包含了其它被 ...

  2. 【angular之起步】安装

    人生只有眼前的苟且. 所以为了远方,最近在策划一个大阴谋------做一个自己的网站,杂而全的. 各种胡思乱想了一周,先把页面写完了,没辙,就这个不用费太多脑子. 然后开始重头戏,就卡死了. angu ...

  3. javascript的slice()与splice()方法

    (1)数组和String对象都有slice()方法. //Array var list = ['A','B','C','D','DS']; console.log(list.slice(,));//截 ...

  4. 编程之美Ex1——求二进制中1的个数

    又被阿里机考虐了一次,决定改变策略开始刷题T^T 一个字节(8bit)的无符号整型,求其二进制中的“1”的个数,算法执行效率尽可能高. 最先想到的移位操作,末尾位&00000001,然后右移, ...

  5. Eclipse自动补全设置与Eclipse源代码下载

    以前使用VisualStudio和Sublime写代码的时候有很完善的代码提示,但是最近开始使用Eclipse弄JAVA的时候发现它的代码提示不是很习惯.上网找了一些资料,修改了代码提示的方式,记录在 ...

  6. Codeforces 954H Path Counting 【DP计数】*

    Codeforces 954H Path Counting LINK 题目大意:给你一棵n层的树,第i层的每个节点有a[i]个儿子节点,然后问你树上的简单路径中长度在1~n*2-2之间的每个有多少条 ...

  7. PDF去除签名

    1.创建一个只有一页的PDF,用Acrobat打开.2.使用“文档->插入页面”,把有数字签名的文档插入到那一页后面.3.使用“文档->删除页面”,删除第一页,然后保存文档.

  8. gradle 构建包含源码配置

    参考配置: apply plugin: "idea" apply plugin: "groovy" apply plugin: "eclipse&qu ...

  9. Opencv2.2 移植到am335x-y开发板

    1.虚拟机上运行cmake-gui,报找不到文件,指示安装. 2.下载opencv2.2.0 http://opencv.org/downloads.html 3.cmake-gui,配置参考< ...

  10. CRC全套~~~ 转载

    经测试CRC16-CCITT是可以了,其它暂时没有测试哦. 00 0E 00 01 00 01 20 17 12 26 20 19 16 01 00 00 01 01 00 00 00 00 00 0 ...