@@ -64,7 +64,7 @@ pop或者top的时候:
64
64
65
65
## 代码
66
66
67
- * 语言支持:JS,Python
67
+ * 语言支持:JS,C++,Java, Python
68
68
69
69
Javascript Code:
70
70
@@ -139,6 +139,123 @@ MinStack.prototype.min = function() {
139
139
*/
140
140
```
141
141
142
+ C++ Code:
143
+
144
+ ``` c++
145
+ class MinStack {
146
+ stack<long > data;
147
+ long min = INT_MAX;
148
+ public:
149
+ /** initialize your data structure here. * /
150
+ MinStack() {
151
+
152
+ }
153
+
154
+ void push(int x) {
155
+ data.push(x - min);
156
+ if(x < min)
157
+ {
158
+ min = x;
159
+ }
160
+
161
+ }
162
+
163
+ void pop() {
164
+ long top = data.top();
165
+ data.pop();
166
+ // 更新最小值
167
+ if(top < 0)
168
+ {
169
+ min -= top;
170
+ }
171
+
172
+ }
173
+
174
+ int top() {
175
+ long top = data.top();
176
+ // 最小值为 min
177
+ if (top < 0)
178
+ {
179
+ return min;
180
+ }
181
+ else{
182
+ return min+top;
183
+ }
184
+ }
185
+
186
+ int getMin() {
187
+ return min;
188
+ }
189
+ };
190
+
191
+ /**
192
+ * Your MinStack object will be instantiated and called as such:
193
+ * MinStack* obj = new MinStack();
194
+ * obj->push(x);
195
+ * obj->pop();
196
+ * int param_3 = obj->top();
197
+ * int param_4 = obj->getMin();
198
+ * /
199
+ ```
200
+
201
+
202
+ Java Code:
203
+
204
+ ```java
205
+ class MinStack {
206
+ long min;
207
+ Stack<Long> stack;
208
+
209
+ /** initialize your data structure here. */
210
+ public MinStack() {
211
+ stack = new Stack<>();
212
+ }
213
+
214
+ public void push(int x) {
215
+ if (stack.isEmpty()) {
216
+ stack.push(0L);
217
+ min = x;
218
+ }
219
+ else {
220
+ stack.push(x - min);
221
+ if (x < min)
222
+ min = x;
223
+ }
224
+ }
225
+
226
+ public void pop() {
227
+ long p = stack.pop();
228
+
229
+ if (p < 0) {
230
+ // if (p < 0), the popped value is the min
231
+ // Recall p is added by this statement: stack.push(x - min);
232
+ // So, p = x - old_min
233
+ // old_min = x - p
234
+ // again, if (p < 0), x is the min so:
235
+ // old_min = min - p
236
+ min = min - p;
237
+ }
238
+ }
239
+
240
+ public int top() {
241
+ long p = stack.peek();
242
+
243
+ if (p < 0) {
244
+ return (int) min;
245
+ }
246
+ else {
247
+ // p = x - min
248
+ // x = p + min
249
+ return (int) (p + min);
250
+ }
251
+ }
252
+
253
+ public int getMin() {
254
+ return (int) min;
255
+ }
256
+ }
257
+ ```
258
+
142
259
Python Code:
143
260
144
261
``` python
@@ -207,7 +324,7 @@ class MinStack:
207
324
208
325
## 代码
209
326
210
- JavaScript:
327
+ JavaScript Code:
211
328
212
329
``` js
213
330
/**
@@ -263,8 +380,107 @@ MinStack.prototype.min = function() {
263
380
*/
264
381
```
265
382
383
+ C++ Code:
384
+ ``` c++
385
+ class MinStack {
386
+ stack<int > data;
387
+ stack<int > helper;
388
+ public:
389
+ /** initialize your data structure here. * /
390
+ MinStack() {
391
+
392
+ }
393
+
394
+ void push(int x) {
395
+ data.push(x);
396
+ if(helper.empty() || helper.top() >= x)
397
+ {
398
+ helper.push(x);
399
+ }
400
+
401
+ }
402
+
403
+ void pop() {
404
+ int top = data.top();
405
+ data.pop();
406
+ if(top == helper.top())
407
+ {
408
+ helper.pop();
409
+ }
410
+
411
+ }
412
+
413
+ int top() {
414
+ return data.top();
415
+ }
416
+
417
+ int getMin() {
418
+ return helper.top();
419
+ }
420
+ };
421
+
422
+ /**
423
+ * Your MinStack object will be instantiated and called as such:
424
+ * MinStack* obj = new MinStack();
425
+ * obj->push(x);
426
+ * obj->pop();
427
+ * int param_3 = obj->top();
428
+ * int param_4 = obj->getMin();
429
+ * /
430
+ ```
431
+
432
+ Java Code:
433
+ ```java
434
+ public class MinStack {
435
+
436
+ // 数据栈
437
+ private Stack<Integer> data;
438
+ // 辅助栈
439
+ private Stack<Integer> helper;
440
+
441
+ /**
442
+ * initialize your data structure here.
443
+ */
444
+ public MinStack() {
445
+ data = new Stack<>();
446
+ helper = new Stack<>();
447
+ }
448
+
449
+ public void push(int x) {
450
+ // 辅助栈在必要的时候才增加
451
+ data.add(x);
452
+ if (helper.isEmpty() || helper.peek() >= x) {
453
+ helper.add(x);
454
+ }
455
+ }
456
+
457
+ public void pop() {
458
+ // 关键 3:data 一定得 pop()
459
+ if (!data.isEmpty()) {
460
+ // 注意:声明成 int 类型,这里完成了自动拆箱,从 Integer 转成了 int,
461
+ // 因此下面的比较可以使用 "==" 运算符
462
+ int top = data.pop();
463
+ if(top == helper.peek()){
464
+ helper.pop();
465
+ }
466
+ }
467
+ }
468
+
469
+ public int top() {
470
+ if(!data.isEmpty()){
471
+ return data.peek();
472
+ }
473
+ }
474
+
475
+ public int getMin() {
476
+ if(!helper.isEmpty()){
477
+ return helper.peek();
478
+ }
479
+ }
480
+ }
481
+ ```
266
482
267
- Python3:
483
+ Python3 Code :
268
484
269
485
``` python
270
486
class MinStack :
0 commit comments