-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_232.java
56 lines (47 loc) · 1.36 KB
/
LeetCode_232.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
import java.util.Stack;
public class LeetCode_232 {
class MyQueue {
public static Stack<Integer> stack_1;
public static Stack<Integer> stack_2;
public MyQueue() {
stack_1 = new Stack<>();
stack_2 = new Stack<>();
}
public void push(int x) {
stack_1.push(x);
}
public int pop() {
stack_2.clear();
reserse(stack_1, stack_2);
int num = stack_2.pop();
stack_1.clear();
reserse(stack_2, stack_1);
return num;
}
public int peek() {
if (!stack_2.isEmpty()) {
return stack_2.peek();
} else {
reserse(stack_1, stack_2);
return stack_2.peek();
}
}
public boolean empty() {
return stack_1.isEmpty() && stack_2.isEmpty();
}
public void reserse(Stack<Integer> stack_1, Stack<Integer> stack_2) {
Stack<Integer> stack = new Stack<>();
stack.addAll(stack_1);
while(!stack.isEmpty())
stack_2.push(stack.pop());
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
}