-
Notifications
You must be signed in to change notification settings - Fork 2
/
Que225.java
58 lines (43 loc) · 1.26 KB
/
Que225.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayDeque;
import java.util.Queue;
/**
* @author: hyl
* @date: 2019/07/30
**/
public class Que225 {
class MyStack {
private Queue<Integer> inQueue;
private Queue<Integer> outQueue;
/** Initialize your data structure here. */
public MyStack() {
inQueue = new ArrayDeque<Integer>();
outQueue = new ArrayDeque<Integer>();
}
/** Push element x onto stack. */
public void push(int x) {
inQueue.add(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
while (inQueue.size() != 1){
outQueue.add(inQueue.poll());
}
int tmp = inQueue.poll();
while (outQueue.size() != 0){
inQueue.add(outQueue.poll());
}
return tmp;
}
/** Get the top element. */
public int top() {
while (inQueue.size() != 1){
outQueue.add(inQueue.poll());
}
return inQueue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return inQueue.isEmpty() && outQueue.isEmpty();
}
}
}