/**
* // 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. TextView 点击拨打电话

    点击TextView,拨打电话 TextView属性:android:autoLink="phone" 需要配置文件中的属性 <uses-permission android ...

  2. Strophe.Status的所有值

    ERROR: 0 CONNECTING: 1 CONNFAIL: 2 AUTHENTICATING: 3 AUTHFAIL: 4 CONNECTED: 5 DISCONNECTED: 6 DISCON ...

  3. siciyuan开源项目观光指南

    观光方式: 1.从UI到后台,先针对每个界面UI观察结构 2.再观察如何初始化和注入数据 3.针对2所用的后台方法进行概览 4.对3中方法进行深入研究 5.选择一个模块进行重写 6.重复5,搭建大体框 ...

  4. git push是报Permission denied (publickey)错误解决

    今天晕了半天了,搞了个git工程到github上,以为很简单,因为之前也弄过,那知道搞了大半天都搞不好,一直报如下错误 D:\javawork\ee-0.0.1-SNAPSHOT>git pus ...

  5. oracle数据库输入conn / as sysdba 出现ORA-01031: insufficient privileges + 忘记sys密码如何改密码

    今天忘记了oracle数据库sys用户的密码,想着直接改密码输入conn / as sysdba 出现了ORA-01031: insufficient privileges(权限不足)的错误,到处搜教 ...

  6. ZROI2018提高day4t3

    传送门 分析 我们假设如果一个点是0则它的值为-1,如果一个点是1则值为1,则一个区间的答案便是max(pre[i]+sur[i]),这里的pre[i]表示此区间i点和它之前的的前缀的最大值,sur[ ...

  7. hdu5521 Meeting

    传送门 题目 Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated hi ...

  8. Luogu 4151 [WC2011]最大XOR和路径

    你谷又乱评分…… 首先发现答案只有可能是从$1$开始走到$n$,中间绕若干个环,然后使它取到的异或值最大. 这样子的话我们可以随便先取一条路径,强制选择走这条路径,然后把所有的环都丢进线性基里面去,因 ...

  9. python3-字典的循环

    # Auther: Aaron Fan info = { 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex ...

  10. AbstractBootstrap的研读

    AbstractBootstrap是一个工具类,用来配置和启动Channel的,下面看下AbstractBootstrap的类继承,ServerBootstrap用于启动ServerChannel的, ...