Poison

剑指 Offer 09. 用两个栈实现队列

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
class CQueue {

private final Stack<Integer> inStack;
private final Stack<Integer> outStack;

public CQueue() {
this.inStack = new Stack<>();
this.outStack = new Stack<>();
}

public void appendTail(int value) {
inStack.push(value);
}

public int deleteHead() {
if (!outStack.isEmpty()) {
return outStack.pop();
} else {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}

return outStack.isEmpty() ? -1 : outStack.pop();
}
}

}
Reference

剑指 Offer 09. 用两个栈实现队列