forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
37 lines (34 loc) · 678 Bytes
/
solution.cpp
File metadata and controls
37 lines (34 loc) · 678 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
class MinStack
{
private:
std::stack<int> stack;
std::stack<int> min_stack;
public:
void push(int x)
{
stack.push(x);
if (min_stack.empty() || ((!min_stack.empty()) && x <= min_stack.top()))
{
min_stack.push(x);
}
}
void pop()
{
if (!stack.empty())
{
if (stack.top() == min_stack.top())
min_stack.pop();
stack.pop();
}
}
int top()
{
if (!stack.empty())
return stack.top();
}
int getMin()
{
if (!min_stack.empty())
return min_stack.top();
}
};