class Solution {
public:
//用于栈的push 与 pop
stack<int> s1;
//用于存储最小值min
stack<int> s2;
//将元素入栈
void push(int value) {
s1.push(value);
//空元素或者元素较小,则入栈
if(s2.empty() || s2.top() > value)
s2.push(value);
else
//重复加入栈顶
s2.push(s2.top());
}
//弹出栈顶元素
void pop() {
s1.pop();
s2.pop();
}
//获取栈顶元素
int top() {
return s1.top();
}
//获取栈中最小元素
int min() {
return s2.top();
}
};
标签:函数,min,s2,top,pop,JZ30,push,s1
From: https://www.cnblogs.com/H43724334/p/18156464