栈和队列算法总结1

文章展示了如何使用栈和队列数据结构来实现特定功能,如在LeetCode的题目中,用栈实现队列,用队列实现栈,检查括号的有效性,以及解决逆波兰表达式求值的问题。这些算法涉及基本操作如push、pop、peek和计算表达式值等。
摘要由CSDN通过智能技术生成

232. 用栈实现队列 - 力扣(LeetCode)

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

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

/**
 * @return {number}
 */
MyQueue.prototype.pop = function () {
    this.size--
    if (this.stackOut.length > 0) {
        return this.stackOut.pop()
    } else {
        while (this.stackIn.length > 0) {
            this.stackOut.push(this.stackIn.pop())
        }
        return this.stackOut.pop()
    }

};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function () {
    if (this.stackOut.length > 0) {
        return this.stackOut[this.stackOut.length - 1]
    } else {
        return this.stackIn[0]
    }
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function () {
    return this.size === 0
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */

225. 用队列实现栈 - 力扣(LeetCode)


var MyStack = function () {
  this.queue = []
  this.size = 0
};

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

/**
 * @return {number}
 */
MyStack.prototype.pop = function () {
  for (let i = 0; i < this.size - 1; i++) {
    this.queue.push(this.queue.shift())
  }
  this.size--
  return this.queue.shift()
};

/**
 * @return {number}
 */
MyStack.prototype.top = function () {
  return this.queue[this.size - 1]
};

/**
 * @return {boolean}
 */
MyStack.prototype.empty = function () {
  return this.size === 0
};

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */

20. 有效的括号 - 力扣(LeetCode)

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
  const stack = []
  for (let i = 0; i < s.length; i++) {
    if (s[i] === '(' || s[i] === '[' || s[i] === '{') {
      stack.push(s[i])
    } else if (s[i] === ')') {
      if (stack[stack.length - 1] === '(') {
        stack.pop()
      } else {
        return false
      }
    } else if (s[i] === ']') {
      if (stack[stack.length - 1] === '[') {
        stack.pop()
      } else {
        return false
      }
    } else if (s[i] === '}') {
      if (stack[stack.length - 1] === '{') {
        stack.pop()
      } else {
        return false
      }
    }
  }
  return stack.length === 0
};

1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)

/**
 * @param {string} s
 * @return {string}
 */
var removeDuplicates = function (s) {
  const stack = [s[0]]
  for (let i = 1; i < s.length; i++) {
    if (s[i] === stack[stack.length - 1]) {
      stack.pop()
    } else {
      stack.push(s[i])
    }
  }
  return stack.join('')
};

150. 逆波兰表达式求值 - 力扣(LeetCode)

/**
 * @param {string[]} tokens
 * @return {number}
 */
var evalRPN = function (tokens) {
  const stack = []
  for (let i = 0; i < tokens.length; i++) {
    if (tokens[i] !== '+' && tokens[i] !== '-' && tokens[i] !== '*' && tokens[i] !== '/') {
      stack.push(tokens[i])
    } else {
      const num1 = Number(stack.pop())
      const num2 = Number(stack.pop())
      if (tokens[i] === '+') {
        stack.push(num2 + num1)
      } else if (tokens[i] === '-') {
        stack.push(num2 - num1)
      } else if (tokens[i] === '*') {
        stack.push(num2 * num1)
      } else if (tokens[i] === '/') {
        stack.push(num2 / num1 | 0)
      }
    }
  }
  return stack[0]
};

347. 前 K 个高频元素 - 力扣(LeetCode)

/**
 * @param {string[]} tokens
 * @return {number}
 */
var evalRPN = function (tokens) {
  const stack = []
  for (let i = 0; i < tokens.length; i++) {
    if (tokens[i] !== '+' && tokens[i] !== '-' && tokens[i] !== '*' && tokens[i] !== '/') {
      stack.push(tokens[i])
    } else {
      const num1 = Number(stack.pop())
      const num2 = Number(stack.pop())
      if (tokens[i] === '+') {
        stack.push(num2 + num1)
      } else if (tokens[i] === '-') {
        stack.push(num2 - num1)
      } else if (tokens[i] === '*') {
        stack.push(num2 * num1)
      } else if (tokens[i] === '/') {
        stack.push(num2 / num1 | 0)
      }
    }
  }
  return stack[0]
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值