javascript solution beats 100%


  • 0
    /**
     * @constructor
     * @param {Integer[][]} vec2d
     */
    var Vector2D = function(vec2d) {
        this.list = Array.prototype.concat.apply([],vec2d);
        this.nextIndex = 0;
    };
    
    /**
     * @this Vector2D
     * @returns {boolean}
     */
    Vector2D.prototype.hasNext = function() {
        return this.nextIndex < this.list.length;
    };
    
    /**
     * @this Vector2D
     * @returns {integer}
     */
    Vector2D.prototype.next = function() {
        return this.list[this.nextIndex++];
    };
    
    /**
     * Your Vector2D will be called like this:
     * var i = new Vector2D(vec2d), a = [];
     * while (i.hasNext()) a.push(i.next());
    */
    

    Making use of Array.prototype.concat makes things much easier. The second argument of 'apply' function accepts an arguments list, which is exactly the vec2d we wanted.


  • 1
    S

    This is not O(1) space, which defeats the purpose of the question.


  • 0

Log in to reply
 

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