341. Flatten Nested List Iterator
作者:互联网
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(); } }
标签:ni,helper,List,list,Nested,341,nestedList,public 来源: https://www.cnblogs.com/feiflytech/p/15863823.html