232.Implement Queue using Stacks Posted on 2017-09-28 | In LeetCode Solution 1: acceptedAmortized O(1) operations: pop() and peek(). 12345678910111213141516171819202122232425262728293031323334353637383940class MyQueue { private Stack<Integer> input; private Stack<Integer> output; /** Initialize your data structure here. */ public MyQueue() { input = new Stack<>(); output = new Stack<>(); } /** Push element x to the back of queue. */ public void push(int x) { input.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { if (output.isEmpty()) { while (!input.isEmpty()) { output.push(input.pop()); } } return output.isEmpty()? 1 : output.pop(); } /** Get the front element. */ public int peek() { // similar to pop() if (output.isEmpty()) { while (!input.isEmpty()) { output.push(input.pop()); } } return output.isEmpty()? 1 : output.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { return input.isEmpty() && output.isEmpty(); }}