-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOffer09.java
More file actions
34 lines (29 loc) · 870 Bytes
/
Offer09.java
File metadata and controls
34 lines (29 loc) · 870 Bytes
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
package lcof;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* 剑指Offer 09. 用两个栈实现队列
* 两个栈 stack1 和 stack2.
* 1. push 操作时,直接push进 stack1.
* 2. pop 操作时,先看 stack2 是不是空;如果是空的话,将 stack1 所有的值都反向push进来。然后从 stack2 中pop 出一个数
* @author LBW
*/
public class Offer09 {
private Deque<Integer> stack1;
private Deque<Integer> stack2;
public Offer09() {
stack1 = new ArrayDeque<>();
stack2 = new ArrayDeque<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.isEmpty() ? -1 : stack2.pop();
}
}