@@ -67,21 +67,21 @@ class MinStack {
67
67
mins = new ArrayDeque<> ();
68
68
mins. push(Integer . MAX_VALUE );
69
69
}
70
-
70
+
71
71
public void push (int val ) {
72
72
s. push(val);
73
73
mins. push(Math . min(mins. peek(), val));
74
74
}
75
-
75
+
76
76
public void pop () {
77
77
s. pop();
78
78
mins. pop();
79
79
}
80
-
80
+
81
81
public int top () {
82
82
return s. peek();
83
83
}
84
-
84
+
85
85
public int getMin () {
86
86
return mins. peek();
87
87
}
@@ -137,6 +137,55 @@ class MinStack {
137
137
*/
138
138
```
139
139
140
+ ### ** Go**
141
+
142
+ ``` go
143
+ type MinStack struct {
144
+ stack []int
145
+ minStack []int
146
+ }
147
+
148
+ /* * initialize your data structure here. */
149
+ func Constructor () MinStack {
150
+ return MinStack{
151
+ stack: make ([]int , 0 ),
152
+ minStack: make ([]int , 0 ),
153
+ }
154
+ }
155
+
156
+ func (this *MinStack ) Push (x int ) {
157
+ this.stack = append (this.stack , x)
158
+ if len (this.minStack ) == 0 || x <= this.minStack [len (this.minStack )-1 ] {
159
+ this.minStack = append (this.minStack , x)
160
+ }
161
+ }
162
+
163
+ func (this *MinStack ) Pop () {
164
+ v := this.stack [len (this.stack )-1 ]
165
+ this.stack = this.stack [:len (this.stack )-1 ]
166
+ if v == this.minStack [len (this.minStack )-1 ] {
167
+ this.minStack = this.minStack [:len (this.minStack )-1 ]
168
+ }
169
+ }
170
+
171
+ func (this *MinStack ) Top () int {
172
+ return this.stack [len (this.stack )-1 ]
173
+ }
174
+
175
+ func (this *MinStack ) GetMin () int {
176
+ return this.minStack [len (this.minStack )-1 ]
177
+ }
178
+
179
+ /* *
180
+ * Your MinStack object will be instantiated and called as such:
181
+ * obj := Constructor();
182
+ * obj.Push(x);
183
+ * obj.Pop();
184
+ * param_3 := obj.Top();
185
+ * param_4 := obj.GetMin();
186
+ */
187
+ ```
188
+
140
189
### ** ...**
141
190
142
191
```
0 commit comments