My AC JAVA solution


  • 0
    C

    public class Vector2D {

    Queue<Iterator<Integer>> queue = new LinkedList<Iterator<Integer>>();
    Integer num = null;
    
    public Vector2D(List<List<Integer>> vec2d) {
        for (int i = 0; i < vec2d.size(); i++){
            queue.add(vec2d.get(i).iterator());
        }
    }
    
    public int next() {
        if (hasNext()){
            int val = num;
            num = null;
            return val;
        } else 
            return -1;
    }
    
    public boolean hasNext() {
        if (num != null) return true;
        while (!queue.isEmpty()) {
            Iterator<Integer> iter = queue.peek();
            if (iter.hasNext()){
                num = iter.next();
                return true;
            } else {
                queue.poll();
            } 
            
        }
        return false;
    }
    

    }


Log in to reply
 

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