题解 | 用两个栈实现队列

用两个栈实现队列

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
};

只有出的不为空才能往里面加东西,不然就会把新进来的的出栈

全部评论

相关推荐

joecii:如果没有工资,那可能没有工资是这家公司最小的问题了
找实习记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务