-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155.cpp
More file actions
50 lines (43 loc) · 947 Bytes
/
155.cpp
File metadata and controls
50 lines (43 loc) · 947 Bytes
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
//
// 155.cpp
// LeetCode
//
// Created by 张佐玮 on 15/6/12.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Ttile: Min Stack
//
#include <iostream>
#include <stack>
using namespace std;
class MinStack {
public:
stack<long> myStack;
long minimum = INT_MAX;
void push(int x) {
long val = x - minimum;
this -> myStack.push(val);
if (val < 0) {
this -> minimum = x;
}
}
void pop() {
long val = this -> myStack.top();
this -> myStack.pop();
if (val < 0) {
this -> minimum = this -> minimum - val;
}
}
int top() {
long val = this -> myStack.top();
if (val < 0) {
return (int) this -> minimum;
}
else {
return (int) (this -> minimum + myStack.top());
}
}
int getMin() {
return (int) this -> minimum;
}
};