[LintCode]Implement Queue by Two Stacks
public class Solution {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public Solution() {
// 2015-09-03
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void push(int element) {
stack1.push(element);
}
public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int top() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
}