11
11
12
12
<!-- 这里可写通用的实现逻辑 -->
13
13
14
- 利用辅助栈存放栈的最小元素。
14
+ ** 方法一:双栈**
15
+
16
+ 我们用两个栈来实现,其中` stk1 ` 用来存储数据,` stk2 ` 用来存储当前栈中的最小值。初始时,` stk2 ` 中存储一个极大值。
17
+
18
+ - 当我们向栈中压入一个元素 ` x ` 时,我们将 ` x ` 压入 ` stk1 ` ,并将 ` min(x, stk2[-1]) ` 压入 ` stk2 ` 。
19
+ - 当我们从栈中弹出一个元素时,我们将 ` stk1 ` 和 ` stk2 ` 的栈顶元素都弹出。
20
+ - 当我们要获取当前栈中的栈顶元素时,我们只需要返回 ` stk1 ` 的栈顶元素即可。
21
+ - 当我们要获取当前栈中的最小值时,我们只需要返回 ` stk2 ` 的栈顶元素即可。
22
+
23
+ 时间复杂度:对于每个操作,时间复杂度均为 $O(1)$,空间复杂度 $O(n)$。
15
24
16
25
<!-- tabs:start -->
17
26
@@ -57,39 +66,37 @@ class MinStack:
57
66
58
67
``` java
59
68
class MinStack {
60
- private Deque<Integer > s ;
61
- private Deque<Integer > mins ;
69
+ private Deque<Integer > stk1 = new ArrayDeque<> () ;
70
+ private Deque<Integer > stk2 = new ArrayDeque<> () ;
62
71
63
72
/* * initialize your data structure here. */
64
73
public MinStack () {
65
- s = new ArrayDeque<> ();
66
- mins = new ArrayDeque<> ();
67
- mins. push(Integer . MAX_VALUE );
74
+ stk2. push(Integer . MAX_VALUE );
68
75
}
69
76
70
- public void push (int val ) {
71
- s . push(val );
72
- mins . push(Math . min(mins . peek(), val ));
77
+ public void push (int x ) {
78
+ stk1 . push(x );
79
+ stk2 . push(Math . min(x, stk2 . peek()));
73
80
}
74
81
75
82
public void pop () {
76
- s . pop();
77
- mins . pop();
83
+ stk1 . pop();
84
+ stk2 . pop();
78
85
}
79
86
80
87
public int top () {
81
- return s . peek();
88
+ return stk1 . peek();
82
89
}
83
90
84
91
public int getMin () {
85
- return mins . peek();
92
+ return stk2 . peek();
86
93
}
87
94
}
88
95
89
96
/**
90
97
* Your MinStack object will be instantiated and called as such:
91
98
* MinStack obj = new MinStack();
92
- * obj.push(val );
99
+ * obj.push(x );
93
100
* obj.pop();
94
101
* int param_3 = obj.top();
95
102
* int param_4 = obj.getMin();
@@ -100,36 +107,33 @@ class MinStack {
100
107
101
108
``` cpp
102
109
class MinStack {
103
- private:
104
- stack<int > stk;
105
- stack<int > minStk;
106
-
107
110
public:
108
111
/** initialize your data structure here. * /
109
- MinStack() = default;
112
+ MinStack() {
113
+ stk2.push(INT_MAX);
114
+ }
110
115
111
116
void push(int x) {
112
- if (minStk.empty() || minStk.top() >= x) {
113
- minStk.push(x);
114
- }
115
- stk.push(x);
117
+ stk1.push(x);
118
+ stk2.push(min(x, stk2.top()));
116
119
}
117
120
118
121
void pop () {
119
- int val = stk.top();
120
- stk.pop();
121
- if (val == minStk.top()) {
122
- minStk.pop();
123
- }
122
+ stk1.pop();
123
+ stk2.pop();
124
124
}
125
125
126
126
int top() {
127
- return stk .top();
127
+ return stk1 .top();
128
128
}
129
129
130
130
int getMin() {
131
- return minStk .top();
131
+ return stk2 .top();
132
132
}
133
+
134
+ private:
135
+ stack<int > stk1;
136
+ stack<int > stk2;
133
137
};
134
138
135
139
/* *
@@ -188,39 +192,38 @@ class MinStack {
188
192
189
193
``` go
190
194
type MinStack struct {
191
- stack []int
192
- minStack []int
195
+ stk1 []int
196
+ stk2 []int
193
197
}
194
198
195
199
/* * initialize your data structure here. */
196
200
func Constructor () MinStack {
197
- return MinStack{
198
- stack: make ([]int , 0 ),
199
- minStack: make ([]int , 0 ),
200
- }
201
+ return MinStack{[]int {}, []int {math.MaxInt32 }}
201
202
}
202
203
203
204
func (this *MinStack ) Push (x int ) {
204
- this.stack = append (this.stack , x)
205
- if len (this.minStack ) == 0 || x <= this.minStack [len (this.minStack )-1 ] {
206
- this.minStack = append (this.minStack , x)
207
- }
205
+ this.stk1 = append (this.stk1 , x)
206
+ this.stk2 = append (this.stk2 , min (x, this.stk2 [len (this.stk2 )-1 ]))
208
207
}
209
208
210
209
func (this *MinStack ) Pop () {
211
- v := this.stack [len (this.stack )-1 ]
212
- this.stack = this.stack [:len (this.stack )-1 ]
213
- if v == this.minStack [len (this.minStack )-1 ] {
214
- this.minStack = this.minStack [:len (this.minStack )-1 ]
215
- }
210
+ this.stk1 = this.stk1 [:len (this.stk1 )-1 ]
211
+ this.stk2 = this.stk2 [:len (this.stk2 )-1 ]
216
212
}
217
213
218
214
func (this *MinStack ) Top () int {
219
- return this.stack [len (this.stack )-1 ]
215
+ return this.stk1 [len (this.stk1 )-1 ]
220
216
}
221
217
222
218
func (this *MinStack ) GetMin () int {
223
- return this.minStack [len (this.minStack )-1 ]
219
+ return this.stk2 [len (this.stk2 )-1 ]
220
+ }
221
+
222
+ func min (a , b int ) int {
223
+ if a < b {
224
+ return a
225
+ }
226
+ return b
224
227
}
225
228
226
229
/* *
@@ -287,6 +290,47 @@ impl MinStack {
287
290
*/
288
291
```
289
292
293
+ ### ** C#**
294
+
295
+ ``` cs
296
+ public class MinStack {
297
+ private Stack <int > stk1 = new Stack <int >();
298
+ private Stack <int > stk2 = new Stack <int >();
299
+
300
+ /** initialize your data structure here. */
301
+ public MinStack () {
302
+ stk2 .Push (int .MaxValue );
303
+ }
304
+
305
+ public void Push (int x ) {
306
+ stk1 .Push (x );
307
+ stk2 .Push (Math .Min (x , GetMin ()));
308
+ }
309
+
310
+ public void Pop () {
311
+ stk1 .Pop ();
312
+ stk2 .Pop ();
313
+ }
314
+
315
+ public int Top () {
316
+ return stk1 .Peek ();
317
+ }
318
+
319
+ public int GetMin () {
320
+ return stk2 .Peek ();
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Your MinStack object will be instantiated and called as such:
326
+ * MinStack obj = new MinStack();
327
+ * obj.Push(x);
328
+ * obj.Pop();
329
+ * int param_3 = obj.Top();
330
+ * int param_4 = obj.GetMin();
331
+ */
332
+ ```
333
+
290
334
### ** ...**
291
335
292
336
```
0 commit comments