341. 扁平化嵌套列表迭代器

给你一个嵌套的整型列表。请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数。

列表中的每一项或者为一个整数,或者是另一个列表。其中列表的元素也可能是整数或是其他列表。

示例 1:

输入: [[1,1],2,[1,1]]

输出: [1,1,2,1,1]

解释: 通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,1,2,1,1]。

示例 2:

输入: [1,[4,[6]]]

输出: [1,4,6]

解释: 通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,4,6]。

/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
private List<Integer> list = new ArrayList<>();
private int index; private void add(List<NestedInteger> nestedList) {
for (NestedInteger nestedInteger : nestedList) {
if (nestedInteger.isInteger()) {
list.add(nestedInteger.getInteger());
} else {
add(nestedInteger.getList());
}
}
} public NestedIterator(List<NestedInteger> nestedList) {
add(nestedList);
} @Override
public Integer next() {
return list.get(index++);
} @Override
public boolean hasNext() {
return index < list.size();
}
} /**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/

Java实现 LeetCode 341 扁平化嵌套列表迭代器的更多相关文章

  1. 【python】Leetcode每日一题-扁平化嵌套列表迭代器

    [python]Leetcode每日一题-扁平化嵌套列表迭代器 [题目描述] 给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另 ...

  2. Java实现 LeetCode 430 扁平化多级双向链表

    430. 扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例 ...

  3. Leetcode 430.扁平化多级双向链表

    扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁 ...

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

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

  5. LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly Linked List

    您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁平化列表,使所有结点 ...

  6. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  7. java 扁平化输出json所有节点key/value

    本章主要介绍用java实现扁平化输出json所有节点key/value(包含所有内层子节点) 1.json结构 目的输出bill_list下的datalist里的子节点key/value 2.实现代码 ...

  8. [leetcode]339. Nested List Weight Sum嵌套列表加权和

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  9. 1.AutoMapper核心:扁平化

    对象 - 对象映射的一个常见用法是获取一个复杂的对象模型,并将其展开成一个更简单的模型. 您可以采取复杂的模型,如: public class Order { private readonly ILi ...

随机推荐

  1. LeetCode 102. 二叉树的层序遍历 | Python

    102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...

  2. [hdu5256]LIS模型

    题目:有一个数列A1,A2...An,修改数量最少的元素,使得这个数列严格递增.无论是修改前还是修改后,每个元素都必须是整数. 思路: 修改数量最少的元素使得这个数列严格递增,等价于让数量最多的元素不 ...

  3. Mysql常用sql语句(21)- regexp 正则表达式查询

    测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 正则的强大不言而喻,Mysql中也提供了 reg ...

  4. go 函数 方法 接口

    概论 函数 方法 接口 概论 方法在编译时静态绑定,依托于具体的类型 接口对应的方法是在运行时动态绑定 进程内初始化顺序 初始化导入包的常量和变量(可以导出的变量)--->包的init函数,不同 ...

  5. centos7 下安装apache mysql php phpmyadmin。

    1 安装mysql yum -y install mariadb-server systemctl start mariadb.service systemctl enable mariadb.ser ...

  6. css实现双色饼图

    from:wx--前端早读课 首先回想用css画三角形的方法: <div class="triangle"></div> .triangle { displ ...

  7. 01 基础版web框架

    01 基础版web框架 服务器server端python程序(基础版): import socket server=socket.socket() server.bind(("127.0.0 ...

  8. [转]如何用Vmware 10虚拟化物理机

    在重装系统的过程中总会遇到这种情况,平时用的东西一下子就找不到了,比如办公记录,考勤记录等等.虽然在重装系统之前你一般都会备份一些重要文件,但是往往都会漏备份一些之前忽略文件,还有一些自己使用电脑的“ ...

  9. NIO之路1--传统BIO到NIO的演变过程

    一.BIO时代 BIO1.0版本 在jdk1.4之前,java网络编程都是采用的BIO模式,BIO全称是Blocking IO,也就是阻塞式IO 接下来就以拨打10086客服电话为例,介绍下BIO是如 ...

  10. Scaner对象

    目录 Scaner的基本概念 基本语法: 1.使用next() 的方式来接收字符串(使用频率较少) 2.使用nextLine()的方式来接收字符串 进阶使用(练习题) Scaner的基本概念 之前我们 ...