题目描述:
使用栈实现队列的下列操作:
push(x) — 将一个元素放入队列的尾部。 pop() — 从队列首部移除元素。 peek() — 返回队列首部的元素。 empty() — 返回队列是否为空。
解题思路
维护两个栈stack1和stack2。当 stack2 为空、而 stack1 不为空时,我们需要继续把 stack1 中的元素转移到 stack2 中去,然后再从 stack2 里取元素。也就是说,所有的出队操作都只能依赖 stack2 来完成
代码实现
/*** 初始化构造函数*/const MyQueue = function () {// 初始化两个栈this.stack1 = [];this.stack2 = [];};/*** Push element x to the back of queue.* @param {number} x* @return {void}*/MyQueue.prototype.push = function (x) {// 直接调度数组的 push 方法this.stack1.push(x);};/*** Removes the element from in front of queue and returns that element.* @return {number}*/MyQueue.prototype.pop = function () {// 假如 stack2 为空,需要将 stack1 的元素转移进来if (this.stack2.length <= 0) {// 当 stack1 不为空时,出栈while (this.stack1.length !== 0) {// 将 stack1 出栈的元素推入 stack2this.stack2.push(this.stack1.pop());}}// 为了达到逆序的目的,我们只从 stack2 里出栈元素return this.stack2.pop();};/*** Get the front element.* @return {number}* 这个方法和 pop 唯一的区别就是没有将定位到的值出栈*/MyQueue.prototype.peek = function () {if (this.stack2.length <= 0) {// 当 stack1 不为空时,出栈while (this.stack1.length != 0) {// 将 stack1 出栈的元素推入 stack2this.stack2.push(this.stack1.pop());}}// 缓存 stack2 的长度const stack2Len = this.stack2.length;return stack2Len && this.stack2[stack2Len - 1];};/*** Returns whether the queue is empty.* @return {boolean}*/MyQueue.prototype.empty = function () {// 若 stack1 和 stack2 均为空,那么队列空return !this.stack1.length && !this.stack2.length;};
