LintCode2016年8月22日算法比赛----平面列表
平面列表
题目描述
给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表。
注意事项
如果给定的列表中的要素本身也是一个列表,那么它也可以包含列表。
样例
给定 [1,2,[1,2]],返回 [1,2,1,2]。
给定 [4,[3,[2,[1]]]],返回 [4,3,2,1]。
/**
* // 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) {
// Write your code here
List<Integer>list=new ArrayList<>();
doFlatten(nestedList, list);
return list;
}
public static void doFlatten(List<NestedInteger> nestedList,List<Integer>list){
if(nestedList!=null){
for(NestedInteger nList:nestedList){
if(nList.isInteger()){
list.add(nList.getInteger());
}
else{
doFlatten(nList.getList(), list);
}
}
}
}
}
LintCode2016年8月22日算法比赛----平面列表的更多相关文章
- LintCode2016年8月22日算法比赛----骰子求和
骰子求和 题目描述 扔n个骰子,向上面的数字之和为 S .给定 Given n,请列出所有可能的 S 值及其相应的概率. 样例 给定n=1,返回 [ [1, 0.17], [2, 0.17], [3, ...
- LintCode2016年8月22日算法比赛----将数组重新排序以构造最小值
将数组重新排序以构造最小值 题目描述 给定一个整数数组,请将其重新排序,以构造最小值. 样例 给定[3,32,321],通过将数组重新排序,可构造6个可能性的数字: 3+32+321=332321 3 ...
- LintCode2016年8月22日算法比赛----克隆二叉树
克隆二叉树 题目描述 深度复制一个二叉树. 给定一个二叉树,返回一个它的克隆品. 样例 给定一个二叉树: 1 / \ 2 3 / \ 4 5 返回其相同结构相同数值的克隆二叉树: 1 / \ 2 3 ...
- LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树
中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...
- LintCode2016年8月8日算法比赛----子树
子树 题目描述 有两个不同大小的二叉树:T1有上百万的节点:T2有好几百的节点.请设计一种算法,判定T2是否为T1的子树. 注意事项 若 T1 中存在从节点 n 开始的子树与 T2 相同,我们称 T2 ...
- LintCode2016年8月8日算法比赛----等价二叉树
等价二叉树 题目描述 检查两棵二叉树是否等价.等价意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数据相等. 样例 1 1 / \ / \ 2 2 and 2 2 / / 4 ...
- 12月22日《奥威Power-BI财务报表数据填报》腾讯课堂开课啦
一扇可以通向任何地方的“任意门”,是我们多少人幼时最梦寐以求的道具之一.即使到了现在,工作中的我们还会时不时有“世界那么大,我想去看看”的念头,或者在突然不想工作的时刻,幻想着自己的家门变成了“任意门 ...
- Autodesk 最新开发技术研讨会 -8月22日-Autodesk北京办公室
为了增进与广大中国地区Autodesk产品的二次开发人员的了解与互动,帮助中国地区的Autodesk产品二次开发人员了解Autodesk最新的二次开发技术动向,并获得Autodesk公司专业开发支持顾 ...
- 见见面、聊聊天 - 5月22日晚7点Meetup,三里屯绿树旁酒吧,畅谈云技术和应用
总是邮件.QQ什么的线上聊,让我们见面吧,不怕见光死,呵呵. 我和同事会先抛砖引玉,给大家介绍一下Autodesk几款最新的云技术和解决方案,然后大家就可畅所欲言,自由交流.来自五湖四海的人,为了 ...
随机推荐
- L08-Linux解决Device eth0 does not seem to be present,delaying initialization问题
问题前提: 在VirtualBox中克隆Linux服务器,如下,由Centos6.5_Base克隆得到node01服务器,采用的是完全克隆的方式,克隆时重新初始化MAC地址. 原服务器Centos6. ...
- Maven自动FTP远程部署
参照官网文档: https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploy-ftp.html 1.在pom.xml中加入: ...
- python全栈开发_day17_时间,系统模板和序列化
一:时间模板 1)time 常用功能: time.sleep() time.time() time.strftime() import time print(time.strftime("% ...
- Django中的Session--实现登录
Django中的Session--实现登录 Django Session Session Session 是什么 Session保存在服务端的键值对. 为什么要有 Session Cookie 虽然 ...
- (转)【干货】MySQL 5.7 多实例(多进程)配置教程
原文:https://blog.csdn.net/zougen/article/details/79567744 https://klionsec.github.io/2017/09/20/mysql ...
- IE8下不识别indexOf的问题
1.为Array原型添加indexOf方法(如果学过面向对象,相当于给Array类添加实例方法),方法体如下: //添加数组IndexOf方法 if (!Array.prototype.indexOf ...
- vue遇见better-scroll
better-scroll better-scroll 是一款重点解决移动端(现已支持 PC 端)各种滚动场景需求的插件.它的核心是借鉴的 iscroll 的实现,它的 API 设计基本兼容 iscr ...
- Hadoop Hive概念学习系列之hive三种方式区别和搭建、HiveServer2环境搭建、HWI环境搭建和beeline环境搭建(五)
说在前面的话 以下三种情况,最好是在3台集群里做,比如,master.slave1.slave2的master和slave1都安装了hive,将master作为服务端,将slave1作为服务端. 以 ...
- AMQP技术术语
什么是AMQP AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.基 ...
- BackgroundWorker的简单用法
微软的官方例子 BackgroudWorker就是一个封装好的异步处理类(就是多线程,广泛用于winform开发中) 例子: 1.界面效果: 一个label,两个button 2.Form2.cs的代 ...