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成 ...
随机推荐
- time包 — 汇总
time包学习 package main; import ( "time" "fmt" ) func main() { //time.Time代表一个纳秒精度的 ...
- 使用GIT上传文件,VSCODE使用GIT上传项目
GIT使用方法: 1.安装git 2.设置用户名和邮箱: git config --global user.name="haokan1113" git config --globa ...
- mysql sql 分析
一.SQL 执行时间分析通过找到执行时间长的 SQL 语句,可以直观的发现数据层的效率问题. 1.通过 show processlist 来查看系统的执行情况mysql> show proces ...
- nginx反向代理的一次实践
场景:前端(VUE.js)应用部署在linux服务器,需要支持http和https访问. 问题1:阿里服务器不支持域名访问? 通过域名绑定服务器解决 问题2:如何通过http访问前端 前端通过ngin ...
- [洛谷P5329][SNOI2019]字符串
题目大意:给一个长度为$n$的字符串$s$,字符串$p_i$为字符串$s$去掉第$i$个字符后形成的字符串.请给所有字符串$p_i$排序(相同字符串按编号排序) 题解:先去掉所有连续相同字符,因为它们 ...
- CCF 2016-04-1 折点计数
CCF 2016-04-1 折点计数 题目 问题描述 给定n个整数表示一个商店连续n天的销售量.如果某天之前销售量在增长,而后一天销售量减少,则称这一天为折点,反过来如果之前销售量减少而后一天销售量增 ...
- 上传自己的 NuGet 包
参考:https://blog.csdn.net/gulijiang2008/article/details/41724927 STEP 1:在NuGet上注册并获取API Key STEP 2:下载 ...
- idea: unable to import maven project
新搭建的maven环境,使用idea创建maven项目时,一直提示 unable to import maven project,百度良久未解决 有说关闭防火前的,亲测无效,后看到说是maven-3. ...
- 虚拟机与宿主机可以互相ping通,但是外网不能
http://rickcheung.blog.51cto.com/913220/354429 1.CentOS 修改DNS 修改对应网卡的DNS的配置文件 # vi /etc/resolv.conf ...
- mysql 数据库 规范
目录 mysql 数据库 规范 基础规范 命名规范 表设计规范 字段设计规范 索引设计规范 SQL编写规范 行为规范 mysql 数据库 规范 基础规范 必须使用InnoDB存储引擎 解读:支持事务. ...