Simple intuitive 2 x, y pointers Java


  • 0
    I
    public class Vector2D implements Iterator<Integer> {
    
        private int a, b;
        private List<List<Integer>> vec2d;
        
        public Vector2D(List<List<Integer>> vec2d) {
            this.vec2d = vec2d;
        }
    
        @Override
        public Integer next() {
            return vec2d.get(a).get(b++);
        }
    
        @Override
        public boolean hasNext() {
            
            while(a < vec2d.size() && b >= vec2d.get(a).size()) {
                b = 0;
                a++;
            }
            
            if(a >= vec2d.size()) {
                return false;
            }
            
            return b < vec2d.get(a).size();
        }
    }
    
    /**
     * Your Vector2D object will be instantiated and called as such:
     * Vector2D i = new Vector2D(vec2d);
     * while (i.hasNext()) v[f()] = i.next();
     */
    

Log in to reply
 

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