232.用栈实现队列 225. 用队列实现栈
栈开始啦
今天是基础实现题,但感觉思想还是要掌握,想好栈和队列互相写的过程。以及栈(先进后出)和队列(先进先出)的特点。
232.用栈实现队列
题目描述 && 测试案例
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
解题思路 && 解题代码
var MyQueue = function() {
//使用两个数组的栈方法(push, pop) 实现队列
this.stackIn = []; //用来存储数据
this.stackOut = []; //用来操作模拟数据
};
/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.stackIn.push(x)
};
/**
* @return {number}
*/
MyQueue.prototype.pop = function() {
if(this.stackOut.length > 0) {
return this.stackOut.pop()
}
while(this.stackIn.length) {
this.stackOut.push(this.stackIn.pop())
}
return this.stackOut.pop()
};
/**
* @return {number}
*/
MyQueue.prototype.peek = function() {
const x = this.pop();
this.stackOut.push(x);
return x;
};
/**
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return this.stackIn.length === 0 && this.stackOut.length === 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. 用队列实现栈
题目描述 && 测试案例
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
[“MyStack”, “push”, “push”, “top”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
解题思路 && 解题代码
var MyStack = function() {
//使用数组来模拟队列
this.queue1 = [];
this.queue2 = [];
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.queue1.push(x)
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
if(this.queue2.length > 0) {
return this.queue2.shift()
//shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
} else {
return this.queue1.pop()
}
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
if(this.queue2.length > 0) {
x = this.queue2[0]
} else {
x = this.queue1[this.queue1.length - 1]
}
return x
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.queue1.length == 0 && this.queue2.length == 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()
*/
总结
队列模仿栈,两个队列,是备份关系。
而栈模仿队列,一个做输入,一个做输出。
同时队列模仿栈,使用的是数组的各种方法。
而栈模仿队列主要是,栈的pop, push等方法。
在前端中,此类模拟等工程题还是比较重要的。美团2022年9月时候的秋招中,写过用队列模拟栈。