forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminStack.cpp
47 lines (46 loc) · 815 Bytes
/
minStack.cpp
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
#include <stack>
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
class MinStack {
public:
void push(int x) {
values.push(x);
if (minValues.empty() || x <= minValues.top())
minValues.push(x);
}
void pop() {
int x = values.top();
values.pop();
if (x == minValues.top())
minValues.pop();
}
int top() const {
return values.top();
}
int getMin() const {
return minValues.top();
}
int size() const {
return values.size();
}
private:
stack<int> values;
stack<int> minValues;
};
void test(const MinStack &stack)
{
printf("top = %d, min = %d\n", stack.top(), stack.getMin());
}
int main(int argc, char **argv)
{
MinStack stack;
stack.push(0);
stack.push(1);
stack.push(0);
test(stack);
stack.pop();
test(stack);
return 0;
}