Simple AC Java solution with two pointers without iterator


  • 1
    S

    The idea is pretty straightforward and any comments are welcome

    public class Vector2D implements Iterator<Integer> {
        int x;
        int y;
        List<List<Integer>> vec2d;
        public Vector2D(List<List<Integer>> vec2d) {
            x = 0;
            y = 0;
            this.vec2d = vec2d;
        }
    
        @Override
        public Integer next() {
            return vec2d.get(x).get(y++);
        }
    
        @Override
        public boolean hasNext() {
            while (x < vec2d.size() && y >= vec2d.get(x).size()) {
                x++;
                y = 0;
            }
            return x < vec2d.size() && y < vec2d.get(x).size();
        }
    }
    

Log in to reply
 

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