Javascript Answer - Basic


  • 0
    N
    /**
     * initialize your data structure here.
     */
    var MinStack = function() {
        this.stack = [];
    };
        
    /** 
     * @param {number} x
     * @return {void}
     */
    MinStack.prototype.push = function(x) {
        this.stack.push(x);
    };
    
    /**
     * @return {void}
     */
    MinStack.prototype.pop = function() {
        this.stack.pop();
    };
    
    /**
     * @return {number}
     */
    MinStack.prototype.top = function() {
        return this.stack.concat().pop();
    };
    
    /**
     * @return {number}
     */
    MinStack.prototype.getMin = function() {
        return Math.min.apply(null, this.stack);
    };
    
    /** 
     * Your MinStack object will be instantiated and called as such:
     * var obj = Object.create(MinStack).createNew()
     * obj.push(x)
     * obj.pop()
     * var param_3 = obj.top()
     * var param_4 = obj.getMin()
     */
    

Log in to reply
 

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