Java solution only using iterators


  • 0
    S
    public class Vector2D implements Iterator<Integer> {
        
        Iterator<List<Integer>> it1;
        Iterator<Integer> it2;
    
        public Vector2D(List<List<Integer>> vec2d) {
            it1 = vec2d.iterator();
            if(it1.hasNext()) it2 = it1.next().iterator();
        }
    
        @Override
        public Integer next() {
            return it2.next();
        }
    
        @Override
        public boolean hasNext() {
            if(it2 == null) return false;
            while(!it2.hasNext() && it1.hasNext()) it2 = it1.next().iterator();
            return it2.hasNext();
        }
    }
    

Log in to reply
 

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