用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
// 按照题意,那么应该要创建两个数组
// 然后push是添加到一个数组的末尾
// 然后pop的话,需要借助另一个数组,把之前的队列清空,然后才能弹出末尾元素
var inStack = [],outStack = [];
function push(node) {
// write code here
inStack.push(node);
}
function pop() {
// write code here
if (!outStack.length) {
while (inStack.length) {
outStack.push(inStack.pop());
}
}
return outStack.pop();
}
数据结构——两个栈构建一个队列
最新推荐文章于 2021-10-26 21:54:56 发布