JAVA X Y Solution


  • 0
    Y
    public class Vector2D implements Iterator<Integer> {
        int x = 0;
        int y = 0;
        List<List<Integer>> matrix;
        public Vector2D(List<List<Integer>> vec2d) {
            this.matrix = vec2d;
        }
    
        @Override
        public Integer next() {
            return matrix.get(x).get(y++);
        }
    
        @Override
        public boolean hasNext() {
            if(matrix.size() != 0) {
                if(y == matrix.get(x).size()) {
                    y = 0;
                    x++;
                    while(x < matrix.size() && matrix.get(x).size() == 0) x++;
                }
                if(x < matrix.size())
                    return true;
                else
                    return false;
            }else
                return false;
        }
    }
    
    /**
     * 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.