@@ -64,7 +64,7 @@ minStack.getMin(); --> 返回 -2.
64
64
- 当我们要获取当前栈中的栈顶元素时,我们只需要返回 ` stk1 ` 的栈顶元素即可。
65
65
- 当我们要获取当前栈中的最小值时,我们只需要返回 ` stk2 ` 的栈顶元素即可。
66
66
67
- 每个操作的时间复杂度为 $O(1)$。整体的空间复杂度为 $O(n)$。
67
+ 每个操作的时间复杂度为 $O(1)$。整体的空间复杂度为 $O(n)$,其中 $n$ 为栈中元素的个数 。
68
68
69
69
<!-- tabs:start -->
70
70
@@ -79,9 +79,9 @@ class MinStack:
79
79
self .stk1 = []
80
80
self .stk2 = [inf]
81
81
82
- def push (self , x : int ) -> None :
83
- self .stk1.append(x )
84
- self .stk2.append(min (x , self .stk2[- 1 ]))
82
+ def push (self , val : int ) -> None :
83
+ self .stk1.append(val )
84
+ self .stk2.append(min (val , self .stk2[- 1 ]))
85
85
86
86
def pop (self ) -> None :
87
87
self .stk1.pop()
@@ -96,7 +96,7 @@ class MinStack:
96
96
97
97
# Your MinStack object will be instantiated and called as such:
98
98
# obj = MinStack()
99
- # obj.push(x )
99
+ # obj.push(val )
100
100
# obj.pop()
101
101
# param_3 = obj.top()
102
102
# param_4 = obj.getMin()
@@ -111,14 +111,13 @@ class MinStack {
111
111
private Deque<Integer > stk1 = new ArrayDeque<> ();
112
112
private Deque<Integer > stk2 = new ArrayDeque<> ();
113
113
114
- /* * initialize your data structure here. */
115
114
public MinStack () {
116
115
stk2. push(Integer . MAX_VALUE );
117
116
}
118
117
119
- public void push (int x ) {
120
- stk1. push(x );
121
- stk2. push(Math . min(x , stk2. peek()));
118
+ public void push (int val ) {
119
+ stk1. push(val );
120
+ stk2. push(Math . min(val , stk2. peek()));
122
121
}
123
122
124
123
public void pop () {
@@ -138,7 +137,7 @@ class MinStack {
138
137
/**
139
138
* Your MinStack object will be instantiated and called as such:
140
139
* MinStack obj = new MinStack();
141
- * obj.push(x );
140
+ * obj.push(val );
142
141
* obj.pop();
143
142
* int param_3 = obj.top();
144
143
* int param_4 = obj.getMin();
@@ -150,14 +149,13 @@ class MinStack {
150
149
``` cpp
151
150
class MinStack {
152
151
public:
153
- /** initialize your data structure here. * /
154
152
MinStack() {
155
153
stk2.push(INT_MAX);
156
154
}
157
155
158
- void push(int x ) {
159
- stk1.push(x );
160
- stk2.push(min(x , stk2.top()));
156
+ void push(int val ) {
157
+ stk1.push(val );
158
+ stk2.push(min(val , stk2.top()));
161
159
}
162
160
163
161
void pop () {
@@ -181,55 +179,13 @@ private:
181
179
/* *
182
180
* Your MinStack object will be instantiated and called as such:
183
181
* MinStack* obj = new MinStack();
184
- * obj->push(x );
182
+ * obj->push(val );
185
183
* obj->pop();
186
184
* int param_3 = obj->top();
187
185
* int param_4 = obj->getMin();
188
186
*/
189
187
```
190
188
191
- ### ** TypeScript**
192
-
193
- ``` ts
194
- class MinStack {
195
- stack: number [];
196
- mins: number [];
197
- constructor () {
198
- this .stack = [];
199
- this .mins = [];
200
- }
201
-
202
- push(x : number ): void {
203
- this .stack .push (x );
204
- this .mins .push (Math .min (this .getMin (), x ));
205
- }
206
-
207
- pop(): void {
208
- this .stack .pop ();
209
- this .mins .pop ();
210
- }
211
-
212
- top(): number {
213
- return this .stack [this .stack .length - 1 ];
214
- }
215
-
216
- getMin(): number {
217
- return this .mins .length == 0
218
- ? Infinity
219
- : this .mins [this .mins .length - 1 ];
220
- }
221
- }
222
-
223
- /**
224
- * Your MinStack object will be instantiated and called as such:
225
- * var obj = new MinStack()
226
- * obj.push(x)
227
- * obj.pop()
228
- * var param_3 = obj.top()
229
- * var param_4 = obj.getMin()
230
- */
231
- ```
232
-
233
189
### ** Go**
234
190
235
191
``` go
@@ -238,14 +194,13 @@ type MinStack struct {
238
194
stk2 []int
239
195
}
240
196
241
- /* * initialize your data structure here. */
242
197
func Constructor () MinStack {
243
198
return MinStack{[]int {}, []int {math.MaxInt32 }}
244
199
}
245
200
246
- func (this *MinStack ) Push (x int ) {
247
- this.stk1 = append (this.stk1 , x )
248
- this.stk2 = append (this.stk2 , min (x , this.stk2 [len (this.stk2 )-1 ]))
201
+ func (this *MinStack ) Push (val int ) {
202
+ this.stk1 = append (this.stk1 , val )
203
+ this.stk2 = append (this.stk2 , min (val , this.stk2 [len (this.stk2 )-1 ]))
249
204
}
250
205
251
206
func (this *MinStack ) Pop () {
@@ -271,20 +226,110 @@ func min(a, b int) int {
271
226
/* *
272
227
* Your MinStack object will be instantiated and called as such:
273
228
* obj := Constructor();
274
- * obj.Push(x );
229
+ * obj.Push(val );
275
230
* obj.Pop();
276
231
* param_3 := obj.Top();
277
232
* param_4 := obj.GetMin();
278
233
*/
279
234
```
280
235
236
+ ### ** TypeScript**
237
+
238
+ ``` ts
239
+ class MinStack {
240
+ stk1: number [];
241
+ stk2: number [];
242
+
243
+ constructor () {
244
+ this .stk1 = [];
245
+ this .stk2 = [Infinity ];
246
+ }
247
+
248
+ push(val : number ): void {
249
+ this .stk1 .push (val );
250
+ this .stk2 .push (Math .min (val , this .stk2 [this .stk2 .length - 1 ]));
251
+ }
252
+
253
+ pop(): void {
254
+ this .stk1 .pop ();
255
+ this .stk2 .pop ();
256
+ }
257
+
258
+ top(): number {
259
+ return this .stk1 [this .stk1 .length - 1 ];
260
+ }
261
+
262
+ getMin(): number {
263
+ return this .stk2 [this .stk2 .length - 1 ];
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Your MinStack object will be instantiated and called as such:
269
+ * var obj = new MinStack()
270
+ * obj.push(x)
271
+ * obj.pop()
272
+ * var param_3 = obj.top()
273
+ * var param_4 = obj.getMin()
274
+ */
275
+ ```
276
+
277
+ ### ** JavaScript**
278
+
279
+ ``` js
280
+ var MinStack = function () {
281
+ this .stk1 = [];
282
+ this .stk2 = [Infinity ];
283
+ };
284
+
285
+ /**
286
+ * @param {number} val
287
+ * @return {void}
288
+ */
289
+ MinStack .prototype .push = function (val ) {
290
+ this .stk1 .push (val);
291
+ this .stk2 .push (Math .min (this .stk2 [this .stk2 .length - 1 ], val));
292
+ };
293
+
294
+ /**
295
+ * @return {void}
296
+ */
297
+ MinStack .prototype .pop = function () {
298
+ this .stk1 .pop ();
299
+ this .stk2 .pop ();
300
+ };
301
+
302
+ /**
303
+ * @return {number}
304
+ */
305
+ MinStack .prototype .top = function () {
306
+ return this .stk1 [this .stk1 .length - 1 ];
307
+ };
308
+
309
+ /**
310
+ * @return {number}
311
+ */
312
+ MinStack .prototype .getMin = function () {
313
+ return this .stk2 [this .stk2 .length - 1 ];
314
+ };
315
+
316
+ /**
317
+ * Your MinStack object will be instantiated and called as such:
318
+ * var obj = new MinStack()
319
+ * obj.push(val)
320
+ * obj.pop()
321
+ * var param_3 = obj.top()
322
+ * var param_4 = obj.getMin()
323
+ */
324
+ ```
325
+
281
326
### ** Rust**
282
327
283
328
``` rust
284
329
use std :: collections :: VecDeque ;
285
330
struct MinStack {
286
- stack : VecDeque <i32 >,
287
- min_stack : VecDeque <i32 >,
331
+ stk1 : VecDeque <i32 >,
332
+ stk2 : VecDeque <i32 >,
288
333
}
289
334
290
335
@@ -294,31 +339,30 @@ struct MinStack {
294
339
*/
295
340
impl MinStack {
296
341
297
- /** initialize your data structure here. */
298
342
fn new () -> Self {
299
- Self { stack : VecDeque :: new (), min_stack : VecDeque :: new () }
343
+ Self { stk1 : VecDeque :: new (), stk2 : VecDeque :: new () }
300
344
}
301
345
302
346
fn push (& mut self , x : i32 ) {
303
- self . stack . push_back (x );
304
- if self . min_stack . is_empty () || * self . min_stack . back (). unwrap () >= x {
305
- self . min_stack . push_back (x );
347
+ self . stk1 . push_back (x );
348
+ if self . stk2 . is_empty () || * self . stk2 . back (). unwrap () >= x {
349
+ self . stk2 . push_back (x );
306
350
}
307
351
}
308
352
309
353
fn pop (& mut self ) {
310
- let val = self . stack . pop_back (). unwrap ();
311
- if * self . min_stack . back (). unwrap () == val {
312
- self . min_stack . pop_back ();
354
+ let val = self . stk1 . pop_back (). unwrap ();
355
+ if * self . stk2 . back (). unwrap () == val {
356
+ self . stk2 . pop_back ();
313
357
}
314
358
}
315
359
316
360
fn top (& self ) -> i32 {
317
- * self . stack . back (). unwrap ()
361
+ * self . stk1 . back (). unwrap ()
318
362
}
319
363
320
364
fn get_min (& self ) -> i32 {
321
- * self . min_stack . back (). unwrap ()
365
+ * self . stk2 . back (). unwrap ()
322
366
}
323
367
}
324
368
@@ -338,26 +382,25 @@ impl MinStack {
338
382
public class MinStack {
339
383
private Stack <int > stk1 = new Stack <int >();
340
384
private Stack <int > stk2 = new Stack <int >();
341
-
342
- /** initialize your data structure here. */
385
+
343
386
public MinStack () {
344
387
stk2 .Push (int .MaxValue );
345
388
}
346
-
389
+
347
390
public void Push (int x ) {
348
391
stk1 .Push (x );
349
392
stk2 .Push (Math .Min (x , GetMin ()));
350
393
}
351
-
394
+
352
395
public void Pop () {
353
396
stk1 .Pop ();
354
397
stk2 .Pop ();
355
398
}
356
-
399
+
357
400
public int Top () {
358
401
return stk1 .Peek ();
359
402
}
360
-
403
+
361
404
public int GetMin () {
362
405
return stk2 .Peek ();
363
406
}
0 commit comments