-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_stack.go
109 lines (89 loc) · 1.99 KB
/
min_stack.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package stacks
type MinStack[T any] struct {
comparator Comparator[T]
stack Stack[*MinNode[T]]
}
var _ Stack[any] = &MinStack[any]{}
func NewMinStack[T any](comparator Comparator[T]) *MinStack[T] {
return &MinStack[T]{
comparator: comparator,
stack: NewArrayStack[*MinNode[T]](),
}
}
func (x *MinStack[T]) Push(values ...T) {
for _, value := range values {
node := &MinNode[T]{
value: value,
}
topNode, err := x.stack.PeekE()
if err != nil {
// 栈为空,认为最小值是自己
node.min = value
} else if x.comparator(topNode.min, value) < 0 {
// 当前栈顶的元素比较小
node.min = topNode.min
} else {
node.min = value
}
x.stack.Push(node)
}
}
func (x *MinStack[T]) Pop() T {
e, _ := x.PopE()
return e
}
func (x *MinStack[T]) PopE() (T, error) {
e, err := x.stack.PopE()
if err != nil {
var zero T
return zero, err
}
return e.value, nil
}
func (x *MinStack[T]) Peek() T {
e, _ := x.PeekE()
return e
}
func (x *MinStack[T]) PeekE() (T, error) {
e, err := x.stack.PeekE()
if err != nil {
var zero T
return zero, err
}
return e.value, nil
}
func (x *MinStack[T]) IsEmpty() bool {
return x.stack.IsEmpty()
}
func (x *MinStack[T]) IsNotEmpty() bool {
return x.stack.IsNotEmpty()
}
func (x *MinStack[T]) Size() int {
return x.stack.Size()
}
func (x *MinStack[T]) Clear() {
x.stack.Clear()
}
func (x *MinStack[T]) String() string {
return x.stack.String()
}
func (x *MinStack[T]) GetMin() T {
e, _ := x.GetMinE()
return e
}
func (x *MinStack[T]) GetMinE() (T, error) {
e, err := x.stack.PeekE()
if err != nil {
var zero T
return zero, err
}
return e.min, nil
}
// ------------------------------------------------ ---------------------------------------------------------------------
type MinNode[T any] struct {
// 栈的当前位置的值
value T
// 栈底到当前位置的最小值
min T
}
// ------------------------------------------------ ---------------------------------------------------------------------