解题思路
-
使用两个栈(s1,s2)进行模拟队列,入队时使用s2保存,出队时把s2栈,进行依次出栈。
-
出栈的元素依次进行入栈到s1,然后s1出栈就是正确的队列模式下应该出队的元素首位。
-
原理是利用s1可以对s2栈入队元素的逆序,就可以在外在表现上正确模拟队列的操作,先入先出。
var MyQueue = function() {
this.s1 = []; // 出队存储区
this.s2 = []; // 入队存储区
};
/**
* Push element x to the back of queue.
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.s2.push(x);
};
/**
* Removes the element from in front of queue and returns that element.
* @return {number}
*/
MyQueue.prototype.pop = function() {
this.transfer(); // 转移栈中元素
const ret = this.s1.pop(); // 出队
return ret;
};
/**
* Get the front element.
* @return {number}
*/
MyQueue.prototype.peek = function() {
this.transfer();
return this.s1[this.s1.length - 1];
};
/**
* Returns whether the queue is empty.
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return this.s1.length === 0 && this.s2.length === 0;
};
MyQueue.prototype.transfer = function() { // 将s2中的元素添加到s1
if (this.s1.length > 0) return; // s1中存在元素时说明还可以依次出队
while (this.s2.length > 0){
this.s1.push(this.s2.pop());
}
};
/**
* 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()
*/