/**
* // 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. 基于cookie实现用户验证

    #!/usr/bin/env python import tornado.ioloop import tornado.web class IndexHander(tornado.web.Request ...

  2. CSS制作水平垂直居中对齐 多种方式各有千秋

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收 集了几种不同的方式制作垂直居中方法,但每种方法各有千秋 ...

  3. 第6章 使用springboot整合netty搭建后台

    我们不会去使用自增长的id,在现阶段的互联网开发过程中,自增长的id是已经不适用了.在未来随着系统版本的迭代,用户数量的递增,肯定会做分库分表,去做一些相应的切分.在这个时候我们就需要有一个唯一的id ...

  4. Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)

    目录 1 什么是spring框架 2 spring框架的特点 3 spring容器 3.1 什么是spring容器 3.2 spring容器创建对象的编程步骤 3.4 spring容器创建对象的方式 ...

  5. 基于R语言的RRT算法效率统计

  6. Git 之 与Github交互

    我们不可能只在一台电脑上开发,白天在公司用公司电脑,晚上在家可以用自己电脑.但是这个代码怎么让两台电脑同步呢?总不能用U盘复制粘贴.太繁琐. 这里我们就可以找个代码托管的平台,帮我们做这件事. Git ...

  7. [译]Javascript中的for循环

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  8. C++11新标准:constexpr关键字

    一.constexpr意义 将变量声明为constexpr类型以便由编译器来验证变量是否是一个常量表达式(不会改变,在编译过程中就能得到计算结果的表达式).是一种比const更强的约束,这样可以得到更 ...

  9. wampserver2.5 在 Win7 64位下的相关配置备忘

    发现运行和配置这个版本和之前的Wampserver有些差异,特此记录 1).wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b,另外有可能需要安 ...

  10. java24种设计模式

    一.设计模式定义 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式是为了可重用代码.让代码更容易被他人理解并且保证代码可靠性. ...