简单题22-平面列表

作者: Airycode | 来源:发表于2018-05-09 10:21 被阅读24次

描述

给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表。
如果给定的列表中的要素本身也是一个列表,那么它也可以包含列表。
您在真实的面试中是否遇到过这个题? 是
样例

给定 [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) {
        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;
    }
}

【代码实现2-非递归的方式】

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;
    }
}

相关文章

  • 简单题22-平面列表

    描述 给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表。如果给定的列表中...

  • 平面排列(非递归实现)

    平面排列(非递归实现) 给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只包含整数的简单列表...

  • 区块链简介

    区块链技术的结构由具有特定顺序交易的区块列表表示。这些列表可以存储为平面文件(txt. 格式)或简单数据库的形式。...

  • 【Leetcode-973】排序-最接近原点的 K 个点

    2020-11-09 打卡题-最接近原点的 K 个点 我们有一个由平面上的点组成的列表 points。需要从中找出...

  • day-07 容器类型(作业)

    第一题,已知列表,求其中心元素 第二题,已知列表,求其所有元素和 第三题,已知列表,输出所有奇数下表元素 第四题,...

  • 22. 平面列表

    22. 平面列表 描述 笔记 数据 评测 给定一个列表,该列表中的每个要素要么是个列表,要么是整数。将其变成一个只...

  • 1041. 困于环中的机器人(Python)

    更多精彩内容,请关注【力扣简单题】。 题目 难度:★★☆☆☆类型:几何、二维数组 在无限的平面上,机器人最初位于 ...

  • day06 列表作业 2018-07-21

    列表作业第一题 已知一个列表,求列表中心元素。 3 列表作业第二题 已知一个列表,求所有元素和。 55 列表作业第...

  • 48.LeetCode169. 求众数

    标签: 位运算 难度: 简单 题目描述 我的解法 利用散列表,此题没有多大难度。注意 Python 中获取字典...

  • 四棱锥:2016年理数北京卷题17

    四棱锥:2016年理数北京卷题17 (17)(本小题14 分) 如图,在四棱锥 中,平面 平面 , , , ...

网友评论

    本文标题:简单题22-平面列表

    本文链接:https://www.haomeiwen.com/subject/lshnrftx.html