栈和队列

103 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第21天,点击查看活动详情

232. 用栈实现队列

image-20220613092714552

难度 1

题解 1

var MyQueue = function() {
    this.stackIn = [];
    this.stackOut = [];
};

/** 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.stackIn.push(x);
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if(this.stackOut.length){
        return this.stackOut.pop();
    }
    while(this.stackIn.length){
        this.stackOut.push(this.stackIn.pop());
    }
    return this.stackOut.pop();
};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    let x = this.pop();
    this.stackOut.push(x);
    return x;
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return !this.stackIn.length && !this.stackOut.length;
};

复杂度

题解

peek那里需要理解一下, 和 java 不同.java 可和 pop 一样直接复用使用 peek, 这里不行.

225. 用队列实现栈

image-20220613094202723

难度 1

题解 1

var MyStack = function() {
    this.queue1 = [];
    this.queue2 = [];
};

/** 
 * @param {number} x
 * @return {void}
 */
MyStack.prototype.push = function(x) {
    this.queue1.push(x);
};

/**
 * @return {number}
 */
MyStack.prototype.pop = function() {
    if(!this.queue1.length){
        [this.queue1,this.queue2] = [this.queue2,this.queue1];
    }
    while(this.queue1.length>1){
        this.queue2.push(this.queue1.shift());
    }
    return this.queue1.shift();
};

/**
 * @return {number}
 */
MyStack.prototype.top = function() {
    let x = this.pop();
    this.queue1.push(x);
    return x;
};

/**
 * @return {boolean}
 */
MyStack.prototype.empty = function() {
    return !this.queue1.length&&!this.queue2.length;
};

复杂度

题解

和栈实现队列不同的是, 队列是用 queue1 返回, 因为是先进先出而不是先进后出

20. 有效的括号

image-20220613100135405

难度 2

题解 1

var isValid = function(s) {
    const stack = [];
    const map = {
        '[':']',
        '(':')',
        '{':'}'
    };
    for( const x of s){
        if(x in map){
            stack.push(x);
            continue;
        }
        if(map[stack.pop()]!=x) return false;
    }
    return !stack.length;
};

复杂度

题解

这个 for of 非常巧妙

当左括号, 都会放入 stack, 如果是右括号就会进行比较, 如果不匹配就会失败

如果左括号多了也会返回失败

1047. 删除字符串中的所有相邻重复项

image-20220613102951905

难度 2

题解 1

var removeDuplicates = function(s) {
    const stack = [];
    for(const x of s){
        if(stack.length && stack[stack.length-1]==x){
            stack.pop();
        }else{
            stack.push(x);
        }
    }
    return stack.join('');
};

复杂度

题解

官方解答比代码随想录答案更易懂

如果current 的字符和 stack 内的相同, 那么 stack 的pop, 不然就puish

150. 逆波兰表达式求值

image-20220614175415370

难度 3

题解 1

var evalRPN = function(tokens) {
    const stack = [];
    const map = new Map([
        ["+",(a,b)=>a*1+b*1],
        ["-",(a,b)=>b-a],
        ["*",(a,b)=>a*b],
        ["/",(a,b)=>b/a|0],
    ])
    for(const x of tokens){
        if(map.has(x)){
            stack.push(map.get(x)(stack.pop(),stack.pop()))
        }else{
            stack.push(x);
            continue;
        }
    }
    return stack.pop();
};

复杂度

题解

这里比较细节, a*1和 b*1 是因为 tokens 是 string 数组, 所以 a+b 直接相加会成为ab字符串, 而不是数字相加