Java with only iterators 4ms 70%


  • 1
    A
    public class Vector2D implements Iterator<Integer> {
    
    final private Iterator<List<Integer>> master;
    private Iterator<Integer> child = null;
    
    public Vector2D(List<List<Integer>> vec2d) {
        master = vec2d.iterator();
        if (master.hasNext()) {
            child = master.next().iterator();
        }
    }
    
    @Override
    public Integer next() {
        return child.next();
    }
    
    @Override
    public boolean hasNext() {
        if (child != null) {
            if (!child.hasNext()) {
                if (master.hasNext()) {
                    child = master.next().iterator();
                    return hasNext();
                }
            } else {
                return true;
            }
        }
        return false;
    }
    

    }


Log in to reply
 

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