題目描述:定義棧的數據結構,請在該類型中實現一個能夠得到棧最小元素的min函數
class Solution {
public:
stack<int> s, mn;
void push(int value) {
s.push(value);
if(mn.empty())
mn.push(value);
else
{
int m = mn.top();
if(m >= value)
{
mn.push(value);
}
}
}
void pop() {
int num1 = s.top();
s.pop();
if(num1 == mn.top())
{
mn.pop();
}
}
int top() {
return s.top();
}
int min() {
return mn.top();
}
};