/**
* // 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:
vector<int> v;
int cnt;
int nested_size;
NestedIterator(vector<NestedInteger> &nestedList) {
// Initialize your data structure here.
v.clear();
cnt = ;
dfs(nestedList);
nested_size = v.size();
} // @return {int} the next element in the iteration
int next() {
// Write your code here
return v[cnt++];
} // @return {boolean} true if the iteration has more element or false
bool hasNext() {
// Write your code here
if(nested_size){
nested_size = nested_size - ;
return true;
} else {
return false;
}
} void dfs(vector<NestedInteger> nestedList){
for(int i = ; i < nestedList.size(); ++i){
if(nestedList[i].isInteger()){
v.push_back(nestedList[i].getInteger());
} else {
dfs(nestedList[i].getList());
}
}
}
};
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i(nestedList);
* while (i.hasNext()) v.push_back(i.next());
*/

Lintcode 摊平嵌套的列表的更多相关文章

  1. Python列表推导式和嵌套的列表推导式

    列表推导式提供了一个更简单的创建列表的方法.常见的用法是把某种操作应用于序列或可迭代对象的每个元素上,然后使用其结果来创建列表,或者通过满足某些特定条件元素来创建子序列. 例如,假设我们想创建一个平方 ...

  2. 孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法

    孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (同步的语音笔记朗读:https://www.ximalaya.com/keji/1 ...

  3. 前端面试题整理——手写flatern摊平数组

    // flatern 是摊平数组 function flat(arr) { const isDeep = arr.some(item => item instanceof Array) if(! ...

  4. JAVA代码实现嵌套层级列表,POI导出嵌套层级列表

    要实现POI导出EXCEL形如 --A1(LV1) ----B1(LV2) ----B2(LV2) ------C1(LV3) ------C2(LV3) ----B3(LV2) --A1(LV1)

  5. cocos2d在CCScrollView中嵌套CCMenu列表

    在cocos2d中,CCMenuItem经常被当做按钮使用.在有许多条目需要逐行显示,并且点击每个条目都触发对应的事件的需求下,最容易想到的是用CCScrollView嵌套CCMenu. 但默认情况下 ...

  6. 与Scheme共舞

    发表在<程序猿>2007年7月刊上.不log上写帖子不用考虑版面限制,所以这里的帖子比发表的啰嗦点.赵健平编辑,Jacky,和刘未鹏都给了我非常多帮助,在这里一并谢了.免费的Scheme实 ...

  7. Python列表的增删改查排嵌套特殊输出格式

    Python列表的增删改查排嵌套特殊输出格式 一.列表的样子: a = ['q' , 'w' , 'e ', 'r','t'] a为列表名,[ ]为列表内容,' '为列表内的元素,'q'为a[0] 二 ...

  8. 列表操作方法,元祖,for循环嵌套

    li = ['alex','wusir''女神']增 1.增加到列表末位 li.append() 连续增加,输入q停止 li = ['alex','wusir''女神'] while 1: s = i ...

  9. python基础之list列表的增删改查以及循环、嵌套

    Python的列表在JS中又叫做数组,是基础数据类型之一,以[]括起来,以逗号隔开,可以存放各种数据类型.嵌套的列表.对象.列表是有序的,即有索引值,可切片,方便取值.列表的操作和对字符串的操作是一样 ...

随机推荐

  1. 学习计划Python-转载

    作者:闲谈后链接:https://www.zhihu.com/question/29775447/answer/145395619来源:知乎著作权归作者所有,转载请联系作者获得授权. 不过需要说明的是 ...

  2. 使用HttpWebRequest POST 文件,带参数

    public string HttpUploadFile(string url, string file, string paramName, string contentType, NameValu ...

  3. import configparser

  4. day17 14.dao模式介绍

    Web的三层架构,不是MVC,Web层,Service层,DAO层. 之前玩的JSP Servlet JavaBean那是MVC模式,那玩意只是表现层的东西. 转账汇款的例子. 说了这么多有啥用啊,一 ...

  5. docker创建容器打开两个端口

     docker run -d -it --name c6_3 -v :/mnt -p 5000:8000 -p 3000  centos  注释: -v 后面为共享文件夹

  6. centos 安装 python flask 和python3安装flask

    pip install Flask python3安装 pip3 install flask

  7. c语言学习笔记 const变量

    在c语言的编程过程中经常会遇到有常数参加运算的运算,比如这种. int a=100*b; 这个100我们叫常数或者叫常量,但是程序中我们不推荐这种直接写常数的方法,有两个缺点. 第一是程序可读性差. ...

  8. 你可能不知道的pdf的功能

    可以创建交互式的pdf,比如在pdf页面添加一个按钮, 添加一个文本框. 上篇文章说了pdf有可移植性,这是个非常重要的特性,我就想能否把3d模型放入到pdf中,这样即使对方电脑没有3d软件也可以查看 ...

  9. EZOJ #81

    传送门 分析 每次拿a中最大的去匹配b中最小的 至于原因画个图感性思考一下就可以啦 代码 #include<iostream> #include<cstdio> #includ ...

  10. boost::python的使用

    boost::python库是pyhon和c++相互交互的框架,可以再python中调用c++的类和方法,也可以让c++调用python的类和方法   python自身提供了一个Python/C AP ...