Share my java solution


  • 0
    O
    public class Vector2D implements Iterator<Integer> {
        private Iterator<List<Integer>> iterL;
        private Iterator<Integer> iter;
    
        public Vector2D(List<List<Integer>> vec2d) {
            iterL = vec2d != null ? vec2d.iterator() : null;
            iter = (iterL != null && iterL.hasNext()) ? iterL.next().iterator() : null;
        }
    
        @Override
        public Integer next() {
            return iter.next();
        }
    
        @Override
        public boolean hasNext() {
            if (iterL == null || iter == null) return false;
            while (!iter.hasNext() && iterL.hasNext()) iter = iterL.next().iterator();
            return iter.hasNext();
        }
    }

Log in to reply
 

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