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成 ...
随机推荐
- 目标检测算法Faster R-CNN
一:Faster-R-CNN算法组成: 1.PRN候选框提取模块: 2.Fast R-CNN检测模块. 二:Faster-R-CNN框架介绍 三:RPN介绍 3.1训练步骤:1.将图片输入到VGG或Z ...
- Asp.net Core CORS 跨域
本文主要介绍在Asp.net Core采用CORS方式解决跨域 关于跨域的原理介绍可参考Asp.net Web API 解决跨域详解 1 在Startup添加允许跨域的策略 services.AddC ...
- git及gitflow命令备忘
全文xxx表示你的分支名 一.git 删除本地分支 git branch -d xxx 删除远程分支 git push origin --delete xxx 查看所有分支 本地分支 git bran ...
- spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件
spring cloud 读取 配置文件属性值 1.bean @Data public class LocalFileConfig { /** * 文件存储地址 */ private String ...
- Helm命令日常使用
更换仓库 默认的stable仓库地址是:https://kubernetes-charts.storage.googleapis.com 若遇到Unable to get an update from ...
- 在 centos 上安装 virutalbox
目录 简介 centos 6.x 安装 virtual box Step 1 – Add Required Yum Repositories Step 2 – Install Required Pac ...
- sql 数据库实时增量更新
---一下sql可以添加到作业中每秒执行一次 数据过多会消耗性能 --数据表如下,其中字段pid mid time price_type是一个组合主键--pid mid time price pr ...
- bat批处理删除多少天前的文件
@echo off ::演示:删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件. ::如果演示结果无误,把del前面的echo去掉,即可实现真正删除. ::本例需要Win2003/Vist ...
- Windows server 2012 R2下安装sharepoint2013
• 安装windows server 2012 R2 系统,配置IP.系统打补丁,修改主机名.加域后重启.• 安装WEB服务器,勾选windows身份验证 • 安装应用程序服务器 • 安装.NET F ...
- python day 8: re模块补充,导入模块,hashlib模块,字符串格式化,模块知识拾遗,requests模块初识
目录 python day 8 1. re模块补充 2. import模块导入 3. os模块 4. hashlib模块 5. 字符串格式:百分号法与format方法 6. 模块知识拾遗 7. req ...