懒地打字。
class MyQueue
{
Stack<Integer> s;
Stack<Integer> t;
public MyQueue()
{
s=new Stack<Integer>();
t=new Stack<Integer>();
}
// Push element x to the back of queue.
public void push(int x) {
s.push(x);
}
// Removes the element from in front of queue.
public void pop() {
int len=s.size();
for(int i=0;i<len;i++)
{
t.push(s.pop());
}
t.pop();
int slen=t.size();
for(int j=0;j<slen;j++)
{
s.push(t.pop());
}
}
// Get the front element.
public int peek() {
int r = 0;
int len = s.size();
for (int i = 0; i < len; i++) {
t.push(s.pop());
}
r=t.peek();
for (int i = 0; i < len; i++) {
s.push(t.pop());
}
return r;
}
// Return whether the queue is empty.
public boolean empty() {
return s.empty()&&t.empty();
}
}