mirror of https://github.com/doocs/leetcode.git
39 lines
737 B
Java
39 lines
737 B
Java
class MyStack {
|
|
private Deque<Integer> q1 = new ArrayDeque<>();
|
|
private Deque<Integer> q2 = new ArrayDeque<>();
|
|
|
|
public MyStack() {
|
|
}
|
|
|
|
public void push(int x) {
|
|
q2.offer(x);
|
|
while (!q1.isEmpty()) {
|
|
q2.offer(q1.poll());
|
|
}
|
|
Deque<Integer> q = q1;
|
|
q1 = q2;
|
|
q2 = q;
|
|
}
|
|
|
|
public int pop() {
|
|
return q1.poll();
|
|
}
|
|
|
|
public int top() {
|
|
return q1.peek();
|
|
}
|
|
|
|
public boolean empty() {
|
|
return q1.isEmpty();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Your MyStack object will be instantiated and called as such:
|
|
* MyStack obj = new MyStack();
|
|
* obj.push(x);
|
|
* int param_2 = obj.pop();
|
|
* int param_3 = obj.top();
|
|
* boolean param_4 = obj.empty();
|
|
*/
|