最小栈的实现

 //最小栈
        var MinStack = function () {
            this.stack = [];
            this.min_stack = [];
        };
        MinStack.prototype.push = function (x) {
            this.stack.push(x);
            if (x <= this.min_stack[this.min_stack.length - 1] || this.min_stack.length == 0) {
                this.min_stack.push(x);
            }
        };
        MinStack.prototype.pop = function () {
            let out = this.stack.pop();
            if (this.min_stack[this.min_stack.length - 1] === out) {
                this.min_stack.pop(out);
            }
        };
        MinStack.prototype.top = function () {
            return this.stack[this.stack.length - 1];
        };
        MinStack.prototype.GetMin = function () {
            return this.min_stack[this.min_stack.length - 1]
        };
        var arr = new MinStack();
        arr.push(3);
        arr.push(1);
        arr.push(8);
        arr.push(2);
        console.log(arr.GetMin());

上一篇:1107 Social Clusters (复杂并查集)


下一篇:10.9 分析数据库信息