当前位置: 首页 > news >正文

LintCode(22)将一个嵌套集合按照原顺序处理为Integer集合

问题

Given a list, each element in the list can be a list or integer. flatten it into a simply list with integers.

Example

Given [1,2,[1,2]], return [1,2,1,2].

Given [4,[3,[2,[1]]]], return [4,3,2,1].

Challenge

Do it in non-recursive.

Notice

If the element in the given list is a list, it can contain list too.

java解决

/**
 * // 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();
 * }
 * 思路
 * 参数合法性判断
 * 定义结果集合
 * 由于不能使用递归
 * 但是又需要循环,由于不能确定需要循环的深度,需要对源数据做数据处理,简化源数据
 * 将源数据看作一个栈,永远都是对栈顶的元素做判断,如果符合是单个Integer就加入结果集合,并弹出
 * 如果不是,就将栈顶元素遍历出来,倒序压栈
 * 循环判断栈是否为空,如果空就是处理完毕
 * 最后,返回结果集合
 */
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
        //参数合法性判断
        if(nestedList.size() < 1){
            return null;
        }
        //定义结果集合
        List<Integer> result = new ArrayList<Integer>();
        //定义标记变量
        boolean flag = false;
        //循环待处理集合
        while(nestedList.size() > 0){
            //获取栈顶元素的性质
            flag = nestedList.get(0).isInteger();
            //如果是单个Integer,则弹出到结果集合
            if(flag){
                Integer inte = nestedList.remove(0).getInteger();
                result.add(inte);
            }else{
                //如果不是单个Integer
                List<NestedInteger> tmp = nestedList.remove(0).getList();
                //倒序压栈
                for(int i = tmp.size() - 1; i >= 0; i--){
                    nestedList.add(0, tmp.get(i));
                }
            }
        }
        return result;
    }
}

 

相关文章:

  • [one_demo_16]直接插入排序的demo
  • [one_demo_17]使用传统方式实现线程间通信的例子
  • ThreadLocal
  • [one_demo_18]js定时器的示例
  • Java8部分新特性
  • jvm简介
  • mybatis使用foreach处理List中的Map
  • log4j2的配置文件
  • 一个用java的NIO实现的socket的客户端和服务端的demo
  • 使用java的nio的pipe实现两个线程间传送数据的demo
  • org.hibernate.TransactionException: nested transactions not supported异常
  • elasticsearch
  • rancher简介
  • InfluxDB+cAdvisor+Grafana容器管理
  • serviceComb[No schema defined for start.servicecomb.io:]异常
  • EventListener原理
  • GitUp, 你不可错过的秀外慧中的git工具
  • JavaScript服务器推送技术之 WebSocket
  • JS题目及答案整理
  • laravel5.5 视图共享数据
  • LeetCode29.两数相除 JavaScript
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • magento2项目上线注意事项
  • python_bomb----数据类型总结
  • python3 使用 asyncio 代替线程
  • Vultr 教程目录
  • Zsh 开发指南(第十四篇 文件读写)
  • 第2章 网络文档
  • 构建二叉树进行数值数组的去重及优化
  • 聚类分析——Kmeans
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 数据可视化之 Sankey 桑基图的实现
  • 选择阿里云数据库HBase版十大理由
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • # Maven错误Error executing Maven
  • # 数据结构
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #pragma pack(1)
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (九)信息融合方式简介
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (转)Oracle存储过程编写经验和优化措施
  • (转)用.Net的File控件上传文件的解决方案
  • ****Linux下Mysql的安装和配置
  • .NET CLR基本术语
  • .NET Compact Framework 多线程环境下的UI异步刷新
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .net访问oracle数据库性能问题
  • .net流程开发平台的一些难点(1)