Short JAVA solution beating 99.30%


  • 0
    L

    public class Vector2D implements Iterator<Integer> {

    List<List<Integer>> vector;
    int list_idx =0;
    int node_idx=0;
    
    public Vector2D(List<List<Integer>> vec2d) {
        vector = vec2d;    
    }
    
    @Override
    public Integer next() {
        return vector.get(list_idx).get(node_idx++);
    }
    
    @Override
    public boolean hasNext() {
    
        while( (list_idx < vector.size()) && (node_idx==vector.get(list_idx).size()) ){
            node_idx=0;
            list_idx++;
        }
        
        return !(list_idx==vector.size());
    }
    

    }


Log in to reply
 

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