Flatten Nested List Iterator

本文对比两种迭代器实现方法:一种直接展开所有嵌套列表,虽然简单但内存消耗较大;另一种采用栈结构逐层深入处理嵌套列表,更加高效。讨论了不同实现方式的优缺点,并指出特定情况下的局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

这个题目的意思是实现一个迭代器。网上有一个方法是这样实现的。

public class NestedIterator implements Iterator<Integer> {  
    int index = 0;  
    List<Integer> vals;  

    public NestedIterator(List<NestedInteger> nestedList) {  
        vals = parser(nestedList);  
    }  

    public List<Integer> parser(List<NestedInteger> nestedList) {  
        List<Integer> res = new ArrayList<Integer>();  
        for (NestedInteger n : nestedList) {  
            if (n.isInteger()) {  
                res.add(n.getInteger());  
            } else {  
                res.addAll(parser(n.getList()));  
            }  
        }  
        return res;  
    }  

    public Integer next() {  
        if (index < vals.size()) {  
            int val = vals.get(index);  
            index++;  
            return val;  
        } else {  
            return -1;  
        }  
    }  

    public boolean hasNext() {  
        return !(index >= vals.size());  
    }  
}  

这种方法简单粗暴。但是并不是理想的实现方式,因为这样的实现导致内存使用要提高一倍。我自己的实现方式是这样的

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


   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> {

    static Deque<List<NestedInteger>> stack=new LinkedList<List<NestedInteger>>();
    static Deque<Integer> stackIndex=new LinkedList<Integer>();

    List<NestedInteger> nestedList;
    public NestedIterator(List<NestedInteger> nestedList) {
        stack.add(nestedList);
        stackIndex.add(0);
    }

    @Override
    public Integer next() {
        int currIndex=stackIndex.pollLast();
        List<NestedInteger> currList=stack.peekLast();

        NestedInteger currEle=currList.get(currIndex);

        if(!currEle.isInteger())
        {
            stack.addLast(currEle.getList());
            stackIndex.addLast(0);
            return next();
        }
        int result=currEle.getInteger();
        if(currIndex+1>=currList.size())
        {
            stack.pollLast();
        }
        else
        {
            stackIndex.addLast(currIndex+1);
        }
        return result;

    }

    @Override
    public boolean hasNext() {
        if(stack.size()==0)
        {
            return false;
        }
        else
        {
             int currIndex=stackIndex.peekLast();
             List<NestedInteger> currList=stack.peekLast();
             if(!currList.get(currIndex).isInteger()&&currList.get(currIndex).getList()==null)
             {
                 stack.pollLast();
                 if(currIndex+1>=currList.size())
                 {

                     stackIndex.pollLast();
                 }
                 else
                 {
                     stackIndex.pollLast();
                     stackIndex.addLast(currIndex+1);

                 }
                 return hasNext();
             }
        }
        return true;
    }
}

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

不过。这个算法不能处理[1,2,3,[]]这种情况,可能是我没有弄懂[]在oj里面的表示方式。不过只要没有[]这种情况,代码运行正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值