题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

解题代码

import java.util.Stack;

public class Solution{
    Stack<Integer> in = new Stack<>();
    Stack<Integer> out = new Stack<>();
    
    public void push(int node){
        in.push(node);
    }
    
    public int pop(){
        if(out.isEmpty())
            while(!in.isEmpty())
                out.push(in.pop());
        return out.pop();
    }
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。