Lintcode 摊平嵌套的列表
/**
* // 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 摊平嵌套的列表的更多相关文章
- Python列表推导式和嵌套的列表推导式
列表推导式提供了一个更简单的创建列表的方法.常见的用法是把某种操作应用于序列或可迭代对象的每个元素上,然后使用其结果来创建列表,或者通过满足某些特定条件元素来创建子序列. 例如,假设我们想创建一个平方 ...
- 孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法
孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (同步的语音笔记朗读:https://www.ximalaya.com/keji/1 ...
- 前端面试题整理——手写flatern摊平数组
// flatern 是摊平数组 function flat(arr) { const isDeep = arr.some(item => item instanceof Array) if(! ...
- JAVA代码实现嵌套层级列表,POI导出嵌套层级列表
要实现POI导出EXCEL形如 --A1(LV1) ----B1(LV2) ----B2(LV2) ------C1(LV3) ------C2(LV3) ----B3(LV2) --A1(LV1)
- cocos2d在CCScrollView中嵌套CCMenu列表
在cocos2d中,CCMenuItem经常被当做按钮使用.在有许多条目需要逐行显示,并且点击每个条目都触发对应的事件的需求下,最容易想到的是用CCScrollView嵌套CCMenu. 但默认情况下 ...
- 与Scheme共舞
发表在<程序猿>2007年7月刊上.不log上写帖子不用考虑版面限制,所以这里的帖子比发表的啰嗦点.赵健平编辑,Jacky,和刘未鹏都给了我非常多帮助,在这里一并谢了.免费的Scheme实 ...
- Python列表的增删改查排嵌套特殊输出格式
Python列表的增删改查排嵌套特殊输出格式 一.列表的样子: a = ['q' , 'w' , 'e ', 'r','t'] a为列表名,[ ]为列表内容,' '为列表内的元素,'q'为a[0] 二 ...
- 列表操作方法,元祖,for循环嵌套
li = ['alex','wusir''女神']增 1.增加到列表末位 li.append() 连续增加,输入q停止 li = ['alex','wusir''女神'] while 1: s = i ...
- python基础之list列表的增删改查以及循环、嵌套
Python的列表在JS中又叫做数组,是基础数据类型之一,以[]括起来,以逗号隔开,可以存放各种数据类型.嵌套的列表.对象.列表是有序的,即有索引值,可切片,方便取值.列表的操作和对字符串的操作是一样 ...
随机推荐
- Hadoop运行程序不报错只有warn也没反应也不输出结果的解决办法
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...
- python之模块(在命令行当中使用pip install 模块来进行模块的安装)
模块:在程序设计中,为完成某一功能所需的一段程序或子程序:或指能由编译程序.装配程序等处理的独立程序单位. 在我们编程的时候我们如果将所有的文件都放在那个py文件中,我们的py文件会很大,这样也很不好 ...
- elmah数据库sql脚本
/* 错误管理工具 SQL代码 */CREATE TABLE dbo.ELMAH_Error( ErrorId UNIQUEIDENTIFIER NOT NULL, Application NVARC ...
- QT5环境搭建
https://blog.csdn.net/liang19890820/article/details/53931813
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
- Linux expect命令
一.简介 通过Shell可以实现简单的控制流功能,但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如telnet服务器等进行交互的功能.而就使用来实现这种功能的工具.Ex ...
- 单引号和0的ASCII码
单引号的ASCII码为0xfe. 那么0xfefe,就表示''. 0的ACSII码为0x30.
- js定时任务
<input type="button" id="btn" value="保存图片" onclick="settime(th ...
- python-ASCII与Unicode
# Auther: Aaron Fan'''ASCII:不支持中文,1个英文占1个字节Unicode(万国码,支持所有国家的文字显示):支持中文,但是每个英文和中文都占2个字节UTF-8(是一种针对U ...
- SDUT 3341 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知二叉 ...