-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCQueue.java
More file actions
52 lines (47 loc) · 1.29 KB
/
CQueue.java
File metadata and controls
52 lines (47 loc) · 1.29 KB
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
package swordPointOffer;
import java.util.Stack;
/**
* @Author: Wenhang Chen
* @Description:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
* <p>
*
* <p>
* 示例 1:
* <p>
* 输入:
* ["CQueue","appendTail","deleteHead","deleteHead"]
* [[],[3],[],[]]
* 输出:[null,null,3,-1]
* 示例 2:
* <p>
* 输入:
* ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
* [[],[],[5],[2],[],[]]
* 输出:[null,-1,null,null,5,2]
* 提示:
* <p>
* 1 <= values <= 10000
* 最多会对 appendTail、deleteHead 进行 10000 次调用
* @Date: Created in 8:37 3/25/2020
* @Modified by:
*/
public class CQueue {
Stack<Integer> stIn;
Stack<Integer> stOut;
public CQueue() {
stIn = new Stack<>();
stOut = new Stack<>();
}
public void appendTail(int value) {
stIn.push(value);
}
public int deleteHead() {
if (stOut.isEmpty()) {
if (stIn.isEmpty()) return -1;
while (!stIn.isEmpty()) {
stOut.push(stIn.pop());
}
}
return stOut.pop();
}
}