-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMyQueue3.java
48 lines (36 loc) · 1.05 KB
/
MyQueue3.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
import java.util.Stack;
class MyQueue3 {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
int front;
/** Initialize your data structure here. */
public MyQueue3() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
if(stack1.isEmpty())
front = x;
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
// 如果 stack2 不为空,直接返回 stack2 的栈首元素
if(!stack2.isEmpty())
return stack2.pop();
while(stack1.size() > 1)
stack2.push(stack1.pop());
return stack1.pop();
}
/** Get the front element. */
public int peek() {
if(!stack2.isEmpty())
return stack2.peek();
return front;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}