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 ...
随机推荐
- CentOS跨网段访问
centos6.2_64删除虚拟网卡 virbr0 卸载以下组件,然后重启系统 yum remove libvirt yum remove libvirt-python 来源:http://www.i ...
- SpringMvc和servlet简单对比介绍
原文链接:http://www.cnblogs.com/haolnu/p/7294533.html 一.servlet实现登录. 咱们先来看一下servlet实现注册登录. <servlet&g ...
- Linux安装vsftpd总结
我使用的是CentOS6安装的vsftpd,转载请注明出处,以下是我的记录: #查看是否已经安装了vsfptd vsftpd -v #安装 yum -y install vsftpd #创建:chro ...
- 我常用的几个第三方 Python 库
转自:http://blog.csdn.net/gzlaiyonghao/article/details/2966811 wxPython 如果你之前是 windows 程序员,用 MFC 或者 WI ...
- 如何在aspx.cs 里面获取html 控件值
aspx 页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...
- vue中特殊特性
key 预期:number | string key 的特殊属性主要用在 Vue 的虚拟 DOM 算法,在新旧 nodes 对比时辨识 VNodes.如果不使用 key,Vue 会使用一种最大限度减少 ...
- nginx unit 安装试用
1. yum 源 nano /etc/yum.repos.d/unit.repo 内容 [unit] name=unit repo baseurl=https://packages.nginx.org ...
- Linux 利用busybox制作根文件系统
busybox版本:1.17.3 官网下载路径:https://busybox.net/downloads/ 网盘下载路径:https://pan.baidu.com/s/1nvrEa73 密码:7y ...
- 搭建基于hyperledger fabric的联盟社区(八) --Fabric证书解析
一.证书目录解析 通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下: ca: 存放组织的根证书和对应的私 ...
- .NET实现多个不同有效时间Session方案思考
什么是Session?简单讲,Session是一种服务端用于保存每个客户端用户的状态信息的机制.客户端第一次访问时,服务端从分配一个空间专门存储该客户端的信息,后续访问时便可以直接获取或者更新状态信息 ...