Description

Given a list, each element in the list can be a list or integer. flatten it into a simply list with integers.

If the element in the given list is a list, it can contain list too.

Example

Example 1:
Input: [[1,1],2,[1,1]]
Output: [1,1,2,1,1] Explanation:
flatten it into a simply list with integers. Example 2:
Input: [1,2,[1,2]]
Output:[1,2,1,2] Explanation:
flatten it into a simply list with integers. Example 3:
Input: [4,[3,[2,[1]]]]
Output:[4,3,2,1] Explanation:
flatten it into a simply list with integers.

思路:

递归处理,每碰到列表就递归处理,每碰到一个数字就添加进答案数组中。

复杂度O(n)

/**
* // 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 Solution { // @param nestedList a list of NestedInteger
// @return a list of integer
public List<Integer> flatten(List<NestedInteger> nestedList) {
List<Integer> result = new ArrayList<Integer>();
for (NestedInteger ele : nestedList)
if (ele.isInteger())
result.add(ele.getInteger());
else
result.addAll(flatten(ele.getList()));
return result;
}
}

  非递归

public class Solution {

    // @param nestedList a list of NestedInteger
// @return a list of integer
public List<Integer> flatten(List<NestedInteger> nestedList) {
boolean isFlat = true;
List<NestedInteger> ls = nestedList;
while (isFlat) {
isFlat = false;
List<NestedInteger> newLs = new ArrayList<>();
for (NestedInteger ni : ls) {
if (ni.isInteger()) {
newLs.add(ni);
} else {
newLs.addAll(ni.getList());
isFlat = true;
}
}
ls = newLs;
}
List<Integer> r = new ArrayList<>();
for (NestedInteger ni : ls) {
r.add(ni.getInteger());
}
return r;
}
}

  

Flatten List的更多相关文章

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

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

  2. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  3. [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  4. 【跟着子迟品 underscore】JavaScript 数组展开以及重要的内部方法 flatten

    Why underscore (觉得这一段眼熟的童鞋可以直接跳到正文了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...

  5. Flatten Binary Tree to Linked List [LeetCode]

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  6. scala学习之: Flatten a nested list structure

    题目要求: (**) Flatten a nested list structure. Example: scala> flatten(List(List(1, 1), 2, List(3, L ...

  7. LeetCode OJ 114. Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  8. [Leetcode][JAVA] Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6   ...

  9. LeetCode - Flatten Binary Tree to Linked List

    题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...

  10. Leetcode 114, Flatten Binary Tree to Linked List

    根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...

随机推荐

  1. Feign输出Info级别日志

    背景 spring cloud netfix组件中,feign相关的日志默认是不会输出的,需要自定义配置才能输出,并且Feign只对Debug基本的日志做出响应, 实际业务需要输出Info级别的日志, ...

  2. Spark源码(1) Spark配置

    写熟悉的第一句代码 val conf = new SparkConf().setAppName("WordCount")点击SparkConf() ,发现 private val ...

  3. SpringCloud入门使用

    目的: 1.springcloud简介 入门案例 2.注册中心eureka springcloud简介 推荐一个springcloud讲解详细的博客:https://blog.csdn.net/qq3 ...

  4. golang ---网卡信息

    package main import ( "fmt" "log" "net" "strings" ) type Net ...

  5. Eclipse 安装反编译插件 Eclipse Class Decompiler

    Eclipse Class Decompiler在线安装方法 https://blog.csdn.net/tangjinquan1157/article/details/77506015 Eclips ...

  6. 2.6_Database Interface JDBC及驱动类型

    JAVA语言参考ODBC,设计专用的数据库连接规范JDBC(JAVA Database Connectivity).目标是让Java开发人员在编写数据库应用程序时,可以有统一的接口,不依赖特定数据库A ...

  7. vue-quill-editor 富文本框使用及上传图片到服务器

    注:上传图片需要结合element-ui的upload上传 首先第一步:安装vue-quill-editor或quill两个模块 yarn add vue-quill-editor -D yarn a ...

  8. ActiveMQ 认证与授权

    使用ActiveMQ自带simpleAuthenticationPlugin 1.直接将用户名密码写入activemq.xml文件 <plugins> <simpleAuthenti ...

  9. Core Animation笔记(- Layer 基本属性)

    一.Layer的基本属性 1. contents 图层内容默认为nil 可以指定一张图片作为内容展示 self.layerView.layer.contents = (__bridge id)imag ...

  10. Spring事务传播机制与隔离机制

    详情查看 https://www.jianshu.com/p/249f2cd42692