题解 | 用两个栈实现队列
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
let stackIn = []
let stackOut = []
function push(node)
{
// write code here
stackIn.push(node)
}
function pop()
{
// write code here
if(stackOut.length === 0){ // 只有出的不为空才能往里面加东西
while(stackIn.length !== 0){
stackOut.push(stackIn.pop())
}
}
return stackOut.pop()
}
module.exports = {
push : push,
pop : pop
};
只有出的不为空才能往里面加东西,不然就会把新进来的的出栈
凡岛公司福利 595人发布