Given a nested list of integers, implement an iterator to flatten it.

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Example 1:
Given the list [[1,1],2,[1,1]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

Example 2:
Given the list [1,[4,[6]]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {
flat(nestedList);
listIter = mList.begin();
} int next() {
int res = *listIter;
++listIter;
return res;
} bool hasNext() {
return listIter != mList.end();
} void flat(vector<NestedInteger>& nL){
for (auto &it : nL) {
if (it.isInteger())
mList.push_back(it.getInteger());
else
flat(it.getList());
}
} private:
list<int>::iterator listIter;
list<int>mList;
};

Design-341. Flatten Nested List Iterator的更多相关文章

  1. [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  2. 341. Flatten Nested List Iterator展开多层数组

    [抄题]: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...

  3. [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  4. 【leetcode】341. Flatten Nested List Iterator

    题目如下: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...

  5. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  6. 341. Flatten Nested List Iterator

    List里可以有int或者List,然后里面的List里面可以再有List. 用Stack来做比较直观 Iterator无非是next()或者hasNext()这2个方程 一开始我想的是hasNext ...

  7. LeetCode 341. Flatten Nested List Iterator

    https://leetcode.com/problems/flatten-nested-list-iterator/

  8. [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  9. [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  10. Leetcode: Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

随机推荐

  1. Js下载文件到本地(兼容多浏览器)

    在客户端通过js下载文件,试过几种下载方式,iframe方式仅限于IE浏览器,window.open(url),location.href=url 这两种方式在chrome浏览器还会是直接打开文件而不 ...

  2. 2. Get the codes from GIT

    Clone the code from git.

  3. ubuntu16.04 LTS Server 安装mysql phpmyadmin apache2 php5.6环境

    1.安装apache sudo apt-get install apache2 为了测试apache2是否正常,访问http://localhost/或http://127.0.0.1/,出现It W ...

  4. 微信分享 apicloud方式 中遇到的坎

    1  appid填错了. 2 图片地址没写对,可能因为拼写问题,图片不存在 3 图片大小有限制.不能太大.

  5. python函数嵌套的实用技术

    def fun(): def fun1(): print () fun1() fun() fun1()#总结老男孩python里面讲过,这个是函数的嵌套,很有用, #效果就是给函数一个自己的小函数.然 ...

  6. VS2010 MFC 使用GDI+给图片添加汉字

    1.配置GDI+ VS2010自带GDI+,直接使用. (1)首先要添加头文件和库 #pragma comment( lib, "gdiplus.lib" ) #include & ...

  7. Realtek 8192cu 支持 Android Hotspot 软ap

    http://www.cnblogs.com/bpasser/archive/2011/10/15/2213483.html Android 2.2 开始增加了WiFi Hotspot,可将Andro ...

  8. 非常实用的windows运行打开服务命令

    1.注册表-->regedit.exe 2.本地服务设置-->services.msc 3.远程桌面连接-->mstsc 4.检查windows版本-->winver 5.组策 ...

  9. 如何通过cmd命令进入到某个硬盘的文件夹

    1.使用快捷键win+R打开运行窗口,并输入cmd回车 2.进入到某个磁盘:在命令提示符中输入d:(代表的的是进入D盘的根目录)并回车 3.接着在cmd中输入dir(dir是directory目录的简 ...

  10. WebService测试工具介绍及下载

    1 LoadRunner  LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问 题,LoadRunner能够对整个企业架构 ...