leetcode341
数据结构设计类题目,参考网上的代码:
/**
* // 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的更多相关文章
- [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
随机推荐
- get、post接口测试-java
public class HttpClient { //get请求方法 public String sendGet(String url, String data) { Date date = new ...
- java基础:eclipse编程不得不知道的技巧
如果你是位具有开发经丰富的工程师,在开发的过程中,你就会很强烈的要求快捷的编程.如何快捷编程,只有更加熟悉开发工具.那么eclipse是同样也有很多技巧.可以带着下面问题来阅读1.如何查找类相关信息? ...
- python(十二):网络编程之ISO/OSI模型
互联网(Internet)是依据操作系统,在计算机硬件的基础上建立起的通讯机制.它依赖于TCP/IP协议栈. 一.ISO/OSI模型 1.ISO七层模型与OSI五层模型 它们将计算机抽象成了具有层级关 ...
- PHP 5.5.38 + mysql 5.0.11 + zabbix3.0 + nginx 安装
PHP 5.5.38 + mysql 5.0.11 + zabbix3.0 + nginx 1.首先在安装好环境下安装 zabbix3.0情况下 2. yum install mysql-devel ...
- [Luogu4233]射命丸文的笔记
luogu description 对于\(x\in[1,n]\)求\(x\)点强联通竞赛图中的哈密顿回路的期望个数膜\(998244353\). \(n\le10^5\) sol 首先\(n\)点竞 ...
- Visual studio环境中的一些快捷键
VS的快键键 F12(转到定义),那怎么转回定义呢? 转回应该是Ctrl+Shift+8 自动排版:ctrl+E+D
- [Drools]JAVA规则引擎 -- Drools- 转http://blog.csdn.net/quzishen/article/details/6163012
[Drools]JAVA规则引擎 -- Drools 标签: java引擎exceptiongetterstringsetter 2011-01-25 14:33 113340人阅读 评论(35) 收 ...
- caddy quic 协议试用&& 几个问题
备注: caddy 具体的安装就不介绍,quic 协议也不介绍了 1. 启用协议,比较简单 /usr/local/bin/caddy -log stdout -quic -conf=/et ...
- Service Mesh 了解
是什么 Service Mesh是专用的基础设施层. 轻量级高性能网络代理. 提供安全的.快速的.可靠地服务间通讯. 与实际应用部署一起但对应用是透明的 作用 提供熔断机制(circuit-break ...
- 【转】linux中shell变量$#,$@,$0,$1,$2的含义解释
原文网址:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html linux中shell变量$#,$@,$0,$1,$2的含义解释: ...