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. 对于Android开发,啥是高级工程师?

    最近一直在思考自己的技术方向.新的技术永远都是层出不穷,kotlin,flutter,小程序,轻应用等等,但是作为一个老鸟,新的东西,永远都是学不完的,想在新的技术上迭代学习出一个新高度,而增加自己的 ...

  2. linux-Centos 7下mysql 5.7.9的rpm包安装

    操作系统:Centos 7.1 mysql数据库版本:mysql5.7.18 1.安装新版mysql之前,我们需要将系统自带的mariadb-lib卸载 [root@123 ~]# rpm -qa|g ...

  3. Mockplus推出真正无限制终身版,做原型就要一辈子!

    如今提到原型工具,各位设计师和PM爸爸们一定不会对Mockplus感到陌生吧?事实上,从一开始的默默无闻,到在UXPA大赛上崭露头角,再到被Adobe XD 列为主要竞品,如今,摩客君已经在全球范围内 ...

  4. 2013.7.15 非html 标签 ,外层 要用 ‘’

    当使用  非  html  标签 是 ,要使用 单引号  作外层 , 双引号 用单层 ,如 <s:if test='direction=="出"'>  可以执行 ,, ...

  5. 深入理解line-height与vertical-align(1)

    http://www.cnblogs.com/xiaohuochai/p/5271217.html

  6. 2018.08.22 NOIP模拟 or(线段树)

    or [描述] 构造一个长度为 n 的非负整数序列 x,满足 m 个条件,第 i 个条件为x[li] | x[li+1] | - | x[ri]=pi. [输入] 第一行两个整数 n,m.接下来 m ...

  7. 2018.08.19 NOIP模拟 dp(二分+状压dp)

    Dp 题目背景 SOURCE:NOIP2015-SHY-10 题目描述 一块土地有 n 个连续的部分,用 H[1],H[2],-,H[n] 表示每个部分的最初高度.有 n 种泥土可用,他们都能覆盖连续 ...

  8. 28. Bad Influence of Western Diet 西式饮食的消极影响

    28. Bad Influence of Western Diet 西式饮食的消极影响 ① The spread of Western eating habits around the world i ...

  9. c语言学生信息管理系统-学习结构体

    #include<stdio.h> #include<stdlib.h> //结构体可以存放的学生信息最大个数,不可变变量 ; //学生信息结构体数组,最多可以存放100个学生 ...

  10. UVa 11491 Erasing and Winning (贪心,单调队列或暴力)

    题意:给一个数字(开头非0),拿掉其中的d个数字,使剩下的数字最大(前后顺序不能变). 析:拿掉d个数字,还剩下n-d个数字.相当于从n个数字中按先后顺序选出n-d个数字使组成的数字最大,当然采用窗口 ...