包含min函数的栈_牛客题霸_牛客网 (nowcoder.com)
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();
}
};