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

/**
* // 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. Git 过滤文件,控制上传

    在Git的版本控制中,可能有些文件是不需要加入控制的,那我们在提交代码时就需要忽略这些文件,下面讲讲应该怎么给Git配置一些忽略规则. 有三种方法可以忽略掉这些文件,这三种方法都能达到目的,只不过适用 ...

  2. cocos2d-x 发布win32 release版本后找不到msvcr110.dll

    解决方法: 安装Visual C++ Redistributable for Visual Studio 2012 //下载地址 http://www.microsoft.com/zh-CN/down ...

  3. 在mac os x 下升级emacs

    大概是09年的时候接触到emacs这个编辑器,当时我们c语言老师用的,他自信满满,而那时我是个vimer,所以每次看他按那么多组合键我就替他感觉手指头累啊. 再后来我用了几年vim写代码,再后来用了许 ...

  4. HDU - 6437:Videos (裸的费用流)

    ...懒得说什么了 #include<bits/stdc++.h> using namespace std; ; <<;int To[maxn],Laxt[maxn],Next ...

  5. org.hibernate.hql.QueryExecutionRequestException:org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations【异常】

    springData学习资料 [http://blog.csdn.net/lw_power/article/details/51296353] [JPA报错]org.springframework.w ...

  6. LOJ2421 NOIP2015 信息传递 【tarjan求最小环】

    LOJ2421 NOIP2015 信息传递 LINK 题目大意就是给你一个有向图,求最小环 有一个很奇妙的性质叫做每个点只有一条出边 然后我们考虑对每个强联通分量进行考虑 发现每个强联通分量内的边数一 ...

  7. BZOJ3747 POI2015 Kinoman 【线段树】*

    BZOJ3747 POI2015 Kinoman Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[ ...

  8. 为iPhone 6设计自适应布局(iOS8)

    Apple从iOS 6加入了Auto Layout后开始就比较委婉的开始鼓励.建议开发者使用自适应布局,但是到目前为止,我感觉大多数开发者一直在回避这个问题,不管是不是由于历史原因造成的,至少他们在心 ...

  9. CentOS常用基础命令汇总

    1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...

  10. python 时间日期处理

    refer to : http://www.wklken.me/posts/2015/03/03/python-base-datetime.html#datetime-string http://ww ...