forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT30.java
More file actions
57 lines (49 loc) · 1.6 KB
/
Copy pathT30.java
File metadata and controls
57 lines (49 loc) · 1.6 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
53
54
55
56
57
package books;
import java.util.Stack;
/**
* @program JavaBooks
* @description: 包含min函数的栈
* @author: mf
* @create: 2019/09/11 10:12
*/
/*
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素
的min函数。在干栈中,调用min、push及pop的时间复杂度都是o(1)
*/
/*
思路
一般来个辅助栈
好多题, 都需要辅助空间的
辅助栈的话,就很简单了。思路也很明了了。
*/
public class T30 {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
Stack<Integer> helpStack = new Stack<>();
dataPush(stack, helpStack, 3);
dataPush(stack, helpStack, 2);
dataPush(stack, helpStack, 1);
dataPush(stack, helpStack, 5);
dataPush(stack, helpStack, 0);
// dataPop(stack, helpStack);
Integer value = dataMin(stack, helpStack);
System.out.println(value);
}
private static Integer dataMin(Stack<Integer> stack, Stack<Integer> helpStack) {
if (stack.isEmpty() || helpStack.isEmpty()) return null;
return helpStack.peek();
}
private static void dataPop(Stack<Integer> stack, Stack<Integer> helpStack) {
if (stack.empty() || helpStack.empty()) return;
stack.pop();
helpStack.pop();
}
private static void dataPush(Stack<Integer> stack, Stack<Integer> helpStack, int value) {
stack.push(value);
if (helpStack.empty() || value < helpStack.peek()) {
helpStack.push(value);
} else {
helpStack.push(helpStack.peek());
}
}
}