Easy to understand beats 95%


  • 0
    M
    public class NestedIterator implements Iterator<Integer> {
    
        List<Integer> intList;
        Iterator<Integer> iterator;
        public NestedIterator(List<NestedInteger> nestedList) {
            intList=new ArrayList<Integer>();
            intList.addAll(calculate(nestedList));
            iterator=intList.iterator();
        }
        
        public List<Integer> calculate(List<NestedInteger> nestedList){
            List<Integer> tempList=new ArrayList<Integer>();
            for(NestedInteger each : nestedList){
                if(each.isInteger())tempList.add(each.getInteger());
                else tempList.addAll(calculate(each.getList()));
            }
            return tempList;
        }
        @Override
        public Integer next() {
            return iterator.next();
        }
    
        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.