扁平化嵌套列表迭代器

Posted Alice_yufeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扁平化嵌套列表迭代器相关的知识,希望对你有一定的参考价值。

/**
 * // 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 empty list if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * 
 */
public class NestedIterator implements Iterator<Integer> 
    private Deque<Integer> deque=new LinkedList<>();
    public NestedIterator(List<NestedInteger> nestedList) 
       dfs(nestedList);
    

    public void dfs(List<NestedInteger> list)
        for (NestedInteger nestedInteger : list) 
            if (nestedInteger.isInteger())
                deque.addLast(nestedInteger.getInteger());
            else 
                dfs(nestedInteger.getList());
            
        
    

    @Override
    public boolean hasNext() 
        return !deque.isEmpty();
    

    @Override
    public Integer next() 
        return deque.pollFirst();
    


/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i = new NestedIterator(nestedList);
 * while (i.hasNext()) v[f()] = i.next();
 */

以上是关于扁平化嵌套列表迭代器的主要内容,如果未能解决你的问题,请参考以下文章

Medium | LeetCode 341. 扁平化嵌套列表迭代器 | 递归 | 栈

扁平化嵌套列表迭代器

341. 扁平化嵌套列表迭代器

java 341.展平嵌套列表迭代器(#)。java

java 341.展平嵌套列表迭代器(#)。java

java 341.展平嵌套列表迭代器(#)。java