LeetCode341、扁平化嵌套列表迭代器

1、题目描述

https://leetcode-cn.com/problems/flatten-nested-list-iterator/

在这里插入图片描述

2、解法

利用多叉树遍历的方法,把所有的值取出按顺序放到一个result列表里面,然后迭代器就取该result list的迭代器实现。

/**
 * // 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 Iterator<Integer> iterator;

    public NestedIterator(List<NestedInteger> nestedList) {
    
    
        //遍历所有的节点
        List<Integer> result = new LinkedList<>();
        for(NestedInteger node:nestedList){
    
    
            traverse(node,result);
        }
        iterator = result.iterator();//获取每个nestedList的迭代器
    }

    @Override
    public Integer next() {
    
    
        return iterator.next();
    }

    @Override
    public boolean hasNext() {
    
    
        return iterator.hasNext();//利用其对应的迭代器
    }
    private void traverse(NestedInteger root,List<Integer>result){
    
    //N叉树的遍历
        if(root.isInteger())//是整数,则是叶子节点
        {
    
    
            result.add(root.getInteger());
        }
        //遍历
        for(NestedInteger child:root.getList()){
    
    
            traverse(child,result);
        }
    }
}

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

运行结果:
在这里插入图片描述
但是上面是我们访问一个元素,就要遍历整颗树的所有结点,把叶子结点的值添加到result。如果规模变大,那么构造器构造时间很长,效率低下。而我们知道迭代器应该是懒加载的。每次取一部分。

进阶解法

我们对每个这样的数据结构进行判断

  • 如果其不是整数就说明它是list,则将其子元素(可能为整数也可能为list)重新加入一个总列表里面,其自身则移除出去
  • 如果是整数就直接返回!list.isEmpty();表明下一次是否可以取数使得该列表总可以保持
/**
 * // 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 LinkedList<NestedInteger> nodeList;
    public NestedIterator(List<NestedInteger> nestedList) {
    
    
        nodeList = new LinkedList(nestedList);//这里使用链式列表
    }
    @Override
    public Integer next() {
    
    
        //hasNext()保证了加入该list的都是整数
        return nodeList.remove(0).getInteger();
    }
    @Override
    public boolean hasNext() {
    
    
       while(!nodeList.isEmpty()&& !nodeList.get(0).isInteger()){
    
    //是列表而不是整数
           //是列表,其不含整数,则把它的子列表取出来,加入链表头中
            List<NestedInteger> first = nodeList.remove(0).getList();
            for(int i=first.size()-1;i>=0;i--){
    
    //遍历其所有的值
                nodeList.addFirst(first.get(i));
            }
        //if(first==null) continue;//取下一个 
       } 
       return !nodeList.isEmpty();//空则说明上面是因为该数不是整数,则说明我们不能取了
    }
}

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

在这里插入图片描述
关于这个效率为什么反而变低了呢?因为数量比较少的情况,直接遍历全部比较快,而使用第二种方式,每次都要执行hashNext里面的大段代码。估计是懒加载累积导致性能变差。

猜你喜欢

转载自blog.csdn.net/qq_44861675/article/details/114299867
今日推荐