剑指offer 05:用两个栈实现队列

时间:2023-03-10 02:39:23
剑指offer 05:用两个栈实现队列

题目描述

用两个栈来实现一个队列,完成队列的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();
}
}