《LeetCode力扣练习》代码随想录——栈与队列(用栈实现队列—Java)
刷题思路来源于 代码随想录
232. 用栈实现队列
-
没有算法
class MyQueue {private Stack<Integer> stackIn;private Stack<Integer> stackOut;public MyQueue() {stackIn=new Stack<>();stackOut=new Stack<>();}public void push(int x) {stackIn.push(x);}public int pop() {this.in2Out();return stackOut.pop();}public int peek() {this.in2Out();return stackOut.peek();}public boolean empty() {return stackIn.empty()&&stackOut.empty();}private void in2Out(){if(!stackOut.empty()){return;}while(!stackIn.empty()){stackOut.push(stackIn.pop());}} }/*** Your MyQueue object will be instantiated and called as such:* MyQueue obj = new MyQueue();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.peek();* boolean param_4 = obj.empty();*/