-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sword21.java
81 lines (69 loc) · 2.01 KB
/
Sword21.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package offer.thirty;
import java.util.Iterator;
import java.util.Stack;
/**
* Created by colin on 2017/3/25.
*/
public class Sword21 {
/* 包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
*/
public static void main(String[] args) {
}
/*
两种方法都没写当前栈为空的情况怎么处理,
*/
static class fun1 {
Stack<Integer> stack = new Stack<>();
/*
只在找最小值时做出判断,其实就是蛮力法
*/
public void push(int node) {
stack.push(node);
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
int min = stack.peek();
int temp = 0;
Iterator<Integer> iterator = stack.iterator();
while (iterator.hasNext()) {
temp = iterator.next();
if (temp < min)
min = temp;
}
return min;
}
}
static class fun2 {
/*
借助辅助栈,辅助栈总依次保存栈中可能的最小值;
*/
Stack<Integer> stack = new Stack<>();
Stack<Integer> min = new Stack<>();
public void push(int node) {
stack.push(node);
//如果压入的是第一个元素或者该元素比辅助栈栈顶元素还小,
//就将该元素其压入
if (min.size() == 0 || node < min.peek())
min.push(node);
//否则的话,压入一个当前的栈顶元素,就是说新来的这个元素不可能成为最小值
else
min.push(min.peek());
}
public void pop() {
stack.pop();
min.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return min.peek();
}
}
}