zoukankan      html  css  js  c++  java
  • 0341. Flatten Nested List Iterator (M)

    Flatten Nested List Iterator (M)

    题目

    Given a nested list of integers, implement an iterator to flatten it.

    Each element is either an integer, or a list -- whose elements may also be integers or other lists.

    Example 1:

    Input: [[1,1],2,[1,1]]
    Output: [1,1,2,1,1]
    Explanation: By calling next repeatedly until hasNext returns false, 
                 the order of elements returned by next should be: [1,1,2,1,1].
    

    Example 2:

    Input: [1,[4,[6]]]
    Output: [1,4,6]
    Explanation: By calling next repeatedly until hasNext returns false, 
                 the order of elements returned by next should be: [1,4,6].
    

    题意

    给定一个任意嵌套的整数列表,要求按顺序输出所有整数。

    思路

    迭代处理:用两个栈分别取存储暂时挂起的列表和对应的下标,每当遇到一个嵌套列表时,就把当前列表和下标压栈,进入嵌套列表继续处理;当当前列表已经遍历完时,则出栈之前的列表和下标继续处理。

    也可直接用递归展开。


    代码实现

    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();
     * }
     */
    
    public class NestedIterator implements Iterator<Integer> {
        private Deque<List<NestedInteger>> lists = new ArrayDeque<>();
        private Deque<Integer> indices = new ArrayDeque<>();
        private List<NestedInteger> list;
        private int index;
        private Integer num;
    
        public NestedIterator(List<NestedInteger> nestedList) {
            list = nestedList;
            index = -1;
            generate();
        }
    
        @Override
        public Integer next() {
            Integer res = num;
            generate();
            return res;
        }
    
        @Override
        public boolean hasNext() {
            return num != null;
        }
    
        private void generate() {
            index++;
    
            while (index < list.size() || !lists.isEmpty()) {
                if (index == list.size()) {
                    list = lists.pop();
                    index = indices.pop() + 1;
                } else if (!list.get(index).isInteger()) {
                    lists.push(list);
                    indices.push(index);
                    list = list.get(index).getList();
                    index = 0;
                } else {
                    break;
                }
            }
    
            num = index < list.size() ? list.get(index).getInteger() : null;
        }
    }
    

    递归展开

    /**
     * // 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 NestedIterator implements Iterator<Integer> {
        List<Integer> list = new ArrayList<>();
        int index = 0;
    
        public NestedIterator(List<NestedInteger> nestedList) {
            generate(nestedList);
        }
    
        @Override
        public Integer next() {
            return list.get(index++);
        }
    
        @Override
        public boolean hasNext() {
            return index != list.size();
        }
    
        private void generate(List<NestedInteger> nestedList) {
            for (NestedInteger item : nestedList) {
                if (item.isInteger()) {
                    list.add(item.getInteger());
                } else {
                    generate(item.getList());
                }
            }
        }
    }
    
  • 相关阅读:
    读书笔记 ASP.NET 2.0编程珠玑
    为什么公司招聘一个好员工很难,程序员找份好工作也不容易
    读书笔记 ASP.NET 2.0高级编程 第31章 配置
    Win7 x64 旗舰版下重新注册IIS7.5
    T_SQL 开发的13个Tips
    报表服务扩展:基于WCF技术的报表服务扩展
    实现多国语言的Reporting Services项目
    技术人生:如何成为一位优秀的程序员
    幸福框架:待实现的基础应用列表
    技术人生:做人十心机
  • 原文地址:https://www.cnblogs.com/mapoos/p/13200936.html
Copyright © 2011-2022 走看看