528. 摊平嵌套的列表

描述

给你一个嵌套的列表,实现一个迭代器将其摊平。
一个列表的每个元素可能是整数或者一个列表。

注意事项

You don't need to implement the remove method.

样例

给出列表 [[1,1],2,[1,1]],经过迭代器之后返回 [1,1,2,1,1]。
给出列表 [1,[4,[6]]],经过迭代器之后返回 [1,4,6]。

知识点

迭代器: Iterator 读文件数据时,用一部分读一部分,没必要全部读取文件,hasNext 用来检测是否存在下一行,并不移动内部光标,next() 取出来这个数,移动光标
对于迭代器,主程序在 hasNext 实现比较好

遍历列表中元素,如果从最左边的一个括号开始展开

代码

/**
 * // 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();
 * }
 */

import java.util.Iterator;

public class NestedIterator implements Iterator<Integer> {

    private Stack<NestedInteger> stack;
    
    // 方法用于把链表中元素加入栈
    // List 里面的元素是一个 NestedInteger ,有自己的定义方法
    private void pushListToStack(List<NestedInteger> nestedList) {
        Stack<NestedInteger> temp = new Stack<>();
        for (NestedInteger nested : nestedList) {
            temp.push(nested);
        }
        
        while (!temp.isEmpty()) {
            stack.push(temp.pop());
        }
    }
    
    // 构造器,将需要摊平的嵌套列表作为形参传递,将列表按照顺序传给 stack
    public NestedIterator(List<NestedInteger> nestedList) {
        stack = new Stack<>();
        pushListToStack(nestedList);
    }

    // @return {int} the next element in the iteration
    @Override
    public Integer next() {
        if (!hasNext()) {
            return null;
        }
        // stack.pop().getInteger() 即把 pop 出来的值用 Integer 的形式来接收
        return stack.pop().getInteger();
    }

    // @return {boolean} true if the iteration has more element or false
    @Override
    public boolean hasNext() {
        // 本步程序为 next() 调用做准备
        // 当 stack 栈顶是一个链表,把链表取出来作为形参传给
        pushListToStack
        while (!stack.isEmpty() && !stack.peek().isInteger()) {
            // stack.pop().getList() 即把 pop 出来的值用 List 的形式来接收
            pushListToStack(stack.pop().getList());
        }
        
        return !stack.isEmpty();
    }
    
    @Override
    public void remove() {}
}

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i = new NestedIterator(nestedList);
 * while (i.hasNext()) v.add(i.next());
 */
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 夜已近半无心入眠 褶皱的窗帘遮蔽了远望的眼 仿佛还有什么比梦境更引人入胜 虽说不喜欢夜的凄凉 但时下的安静不由得带...
    思临阅读 150评论 0 1
  • 晚上,在一个土豪朋友家吃饭,确实是一个土豪,生意做的很偏门,但很大。 以前在一起吃饭时,总觉得他嘻嘻哈哈,什么都不...
    皮皮老猫阅读 264评论 2 4
  • 今天遇到特殊账号登录之后,嵌套的网页打开是空白,运行代码,检测发现代理方法都正常调用,获取本地信息的方法也调用了,...
    未知的远方阅读 233评论 0 0
  • 第一种方法 是对图层进行操作/** 要操作的图片 */ @property (nonatomic,retain) ...
    枫叶知秋阅读 364评论 0 0