This is an itegrator problem.

You can look nestedList as a tree, just recursively read the nestedList, and put the integration into a list, and then itegrate the list, done!

public class NestedIterator implements Iterator<Integer> {
    List<Integer> list = new ArrayList<>();
    Iterator<Integer> it;
    public NestedIterator(List<NestedInteger> nestedList) {
        helper(nestedList);
        it = list.iterator();
        
    }
    
    private void helper(List<NestedInteger> nestedList){
        for(NestedInteger ni: nestedList){
           if(ni.isInteger()){
              list.add(ni.getInteger());
           }
           else{
              helper(ni.getList());
           }
        }
    }

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

    @Override
    public boolean hasNext() {
        return it.hasNext();
    }
}

 

posted on 2022-02-05 07:50  阳光明媚的菲越  阅读(15)  评论(0编辑  收藏  举报