【路飞】化栈为队&&棒球比赛&&比较退格字符串

258 阅读2分钟

记录三道算法题

化栈为队

面试题 03.04. 化栈为队 - 力扣(LeetCode) (leetcode-cn.com)


要求:

    * 用两个数组模拟队列,先进先出。
    * 只能用pop和push方法

栈的特点是后进先出,所以一般可以用在模板解析。vue中就用到了。模拟队列其实只要做到拿出先进去的进行返回就好了,刚好有两个数组,一倒腾就对了,比如进去时 1,2,3,4. 一倒转就是 4,3,2,1了 然后这时候pop 1. 就完成了。

    var MyQueue = function () {
        // 一个存放一个输出
        this.input = []
        this.output = []
      }

      MyQueue.prototype.push = function (x) {
          // 分开了存放和输出后,这里就不用写判断而是直接推
        this.input.push(x)
      }

      MyQueue.prototype.pop = function () {
          // 一个懒处理,思想是 每次存放进 input 数组。
          // 当需要peek或者pop的时候,检查output有没有
          // 如果output没有就把input的全部导入过去。
        if (this.output.length === 0) {
          while (this.input.length) {
            this.output.push(this.input.pop())
          }
        }
        
        // 然后输出output 直到 output用完 就再导一次

        const res = this.output.pop()
        return res
      }

      MyQueue.prototype.peek = function () {
          // 和pop同理,只是不做弹栈操作
        if (this.output.length === 0) {
          while (this.input.length) {
            this.output.push(this.input.pop())
          }
        }

        return this.output[this.output.length - 1]
      }

      MyQueue.prototype.empty = function () {
          // 当两个都为空的时候就是空
        return !this.output.length && !this.input.length
      }

棒球比赛

682. 棒球比赛 - 力扣(LeetCode) (leetcode-cn.com)


要求:

    * 会传入一个数组,为多个依次执行的操作指令
    * '+', 'D', 'C', 任意数字
    

这题还蛮简单的。

    calPoints = function(ops) {
        // 用 for循环也是可以的,也可以用栈来解决。
        let p = ops.reduce((total, curr) => {
            // 推入各个结果
            switch (curr) {
                case '+':
                    total.push(total[total.length - 1] + total[total.length - 2])
                    break
                case 'D':
                    total.push(total[total.length - 1] * 2)
                    break
                case 'C':
                    total.pop()
                    break
                default:
                    total.push(+curr)
                    break
            }
            return total
        }, [])
 
        // 对结果进行求和
        return p.reduce((a, b) => a + b, 0)
    };

比较退格字符串

844. 比较含退格的字符串 - 力扣(LeetCode) (leetcode-cn.com)


要求:

    * 给两个字符串, #代表删除前面的字母。
    * #前面为空的话继续为空。

解析模板用栈是个好方法

    backspaceCompare = function(s, t) {
        function handle(str) {
            const stack = []
            // 收集成一个数组返回
            for(let i = 0; i < str.length; i++) {
                if (str[i] === '#') {
                    stack.pop()
                } else {
                    stack.push(str[i])
                }
            }
            return stack
        }

        s = handle(s)
        t = handle(t)
        
        // 长度不一样就可以直接结束了
        if (s.length !== t.length) {
            return false
        }
        
        // 遍历一遍数组,
        // 优化的点,如果使用指针的话,可以在上面收集的时候同时进行比较
        if (s.length === t.length) {
            for(let i = 0; i < s.length; i++) {
                if (s[i] !== t[i]) {
                    return false
                }
            }
        }
        return true
    };

结束