Flatten List
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的更多相关文章
- [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- [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 ...
- 【跟着子迟品 underscore】JavaScript 数组展开以及重要的内部方法 flatten
Why underscore (觉得这一段眼熟的童鞋可以直接跳到正文了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...
- 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 ...
- scala学习之: Flatten a nested list structure
题目要求: (**) Flatten a nested list structure. Example: scala> flatten(List(List(1, 1), 2, List(3, L ...
- 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 ...
- [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 ...
- 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 ...
- Leetcode 114, Flatten Binary Tree to Linked List
根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...
随机推荐
- bzoj 4500 矩阵 题解
题意: 有一个 $ n * m $ 的矩阵,初始每个格子的权值都为 $ 0 $,可以对矩阵执行两种操作: 选择一行,该行每个格子的权值加1或减1. 选择一列,该列每个格子的权值加1或减1. 现在有 $ ...
- linux全面详细转载文章
在网上发现了一位大佬写的linux各种命令.系统.配置等的详细解析,在此转载保留以便学习! 骏马金龙https://www.cnblogs.com/f-ck-need-u/p/7048359.html
- Django中的admin
1.基本知识 在用Django框架写了一个网站之后,我们添加数据大概有两种方式: 1.在连接的数据库中添加数据 2.登录admin,进入后台添加数据 创建一个Django项目后,我们在url.py中会 ...
- mysql中sum与if,case when 结合使用
1.sum与if结合使用 如图:数据表中,count_money 字段可为正,可为负.为正表示收入,负表示支出. 统计总收入,总支出. select sum(if(count_money > 0 ...
- 一行代码让3D翻转后的文本恢复清晰
FlashPlayer10提供的3D功能有一个相当蛋疼的问题:只要设置过rotationX.rotationY或者rotationZ属性,显示对象里面的文字(尤其是设备字体,位图文本)就会一直处于模糊 ...
- GOF 的23种JAVA常用设计模式 学习笔记 持续更新中。。。。
前言: 设计模式,前人总结下留给后人更好的设计程序,为我们的程序代码提供一种思想与认知,如何去更好的写出优雅的代码,23种设计模式,是时候需要掌握它了. 1.工厂模式 大白话:比如你需要一辆汽车,你无 ...
- SQL Server 2012使用日常
SQL Server 2012个人使用日常(持续完善中) 1.查询筛选 2.修改数据
- NetworkStream的使用(TcpClient,TcpListener)
1.在tcp连接中,Networkstream可以重复读取,重复写入,不用关掉连接. 2.关掉NetworkStream会自动关闭掉Tcp连接 3.NetworkStream不需要使用Flush方法, ...
- Microsoft Visual Studio常用快捷键
快速补全关键字 1)tab; 删除整行代码 1)Ctrl + L; 回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键“Ctrl + -”; 2)前进到下一个光标位置:“Ct ...
- js --策略模式
策略模式的定义: 将算法一个个的单独进行封装,并且使他们可以相互替换.此模式让算法的变化不会影响到使用算法的客户. 先回顾一下,我们在做项目的过程中,是不是经常会遇见因为业务逻辑的关系,我们会写好多的 ...