4 lines Java Solution


  • 0
    L

    public class Vector2D implements Iterator<Integer> {

    private Iterator<List<Integer>> rowIterator;
    private Iterator<Integer> columnIterator;
    
    public Vector2D(List<List<Integer>> vec2d) {
        this.rowIterator = vec2d.iterator();
    }
    
    @Override
    public Integer next() {
        return columnIterator.next();
    }
    
    @Override
    public boolean hasNext() {
        while ((columnIterator == null || !columnIterator.hasNext()) && rowIterator.hasNext()) {
            columnIterator = rowIterator.next().iterator();
        }
        return columnIterator != null && columnIterator.hasNext();
    }
    

    }


Log in to reply
 

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