【路飞】化栈为队

242 阅读1分钟

题目:面试题 03.04. 化栈为队

image.png

分析

用一个栈来存储数据,另外一个栈来临时获取数据,具体分工如下:

  1. 栈A用来存放入队元素
  2. 栈B用来临时获取出队元素
/**
 * Initialize your data structure here.
 */
var MyQueue = function() {
  this.A = [];
  this.B = [];
};

/**
 * Push element x to the back of queue. 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
  this.A.push(x);
};

/**
 * Removes the element from in front of queue and returns that element.
 * @return {number}
 */
MyQueue.prototype.pop = function() {
  while(this.A.length) {
    this.B.push(this.A.pop());
  }
  const head = this.B.pop();
  while(this.B.length) {
    this.A.push(this.B.pop())
  }
  return head;
};

/**
 * Get the front element.
 * @return {number}
 */
MyQueue.prototype.peek = function() {
  return this.A[0]
};

/**
 * Returns whether the queue is empty.
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
  return this.A.length === 0;
};