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成 ...
随机推荐
- son-server模拟http mock数据
json-server 前端开发中,想通过异步请求服务端json数据,但是服务端还没有开发完,此时可以快速启动一个server服务 1,安装json-server插件 npm -g add json- ...
- 【C#】上机实验二
实验1: 求解 1/1 + 1 / 2 + 1 / 3 + 1 / 4 …… + 1 / i = ? 确保精度在 1e-6内. using System; using System.Collect ...
- 在Firefox中操作iframe的一个小问题
在做一个 Web 的打印功能时,需要将被打印的文档写到 iframe 的 document 中. <!doctype html> <html lang="en"& ...
- 2.6_Database Interface JDBC及驱动类型
JAVA语言参考ODBC,设计专用的数据库连接规范JDBC(JAVA Database Connectivity).目标是让Java开发人员在编写数据库应用程序时,可以有统一的接口,不依赖特定数据库A ...
- Python小列子-读取照片位置
Python exifread Python利用exifread库来解析照片的经纬度,对接百度地图API显示拍摄地点. import exifread import re import json im ...
- 【UVA1505】 Flood-it!(IDA*)
题目链接 IDA*,估价函数为当前除了左上角的连通块以外颜色的种类数,因为每次最多消去一个颜色. 维护位于当前连通块的边缘但颜色不同的点,每次从这些点拓展就行. #include <cstdio ...
- Attribute与Property关系
总的来说,其实是HTML Attribute 与 DOM property之间的关系. 1 什么是Property? JS DOM Object对象有property.一个property可能是不同数 ...
- npm/svn 命令
npm npm config set registry https://registry.npm.taobao.org npm config list svn + 清除失败的事务 - cmd管理员运行 ...
- 初始SQL语句 简单使用
初始SQL语句 简单使用 SQL语言共分为四大类: DQL (Data QueryLanguage )数据查询语言 DML(Data manipulation language)数据操纵语言 DDL( ...
- DataPipeline的增量数据支持回滚功能
DataPipeline的增量数据支持回滚功能 第一步:数据任务有增量数据时,回滚按钮激活,允许用户使用该功能进行数据回滚. 第二步:点击回滚按钮,允许用户选择回滚时间或者回滚位置进行数据回滚.选择按 ...