题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
class Solution {
public:
stack<int> s;
stack<int> t;
void push(int value) {
s.push(value);
if(t.empty() || t.top()>value)
t.push(value);
else
{
int tem = t.top();
t.pop();
t.push(value);
t.push(tem);
}
}
void pop() {
int tem = s.top();
s.pop();
if(tem == t.top())t.pop();
else{
int m = t.top();
t.pop();
t.pop();
t.push(m);
}
}
int top() {
return s.top();
}
int min() {
return t.top();
}
};
查看19道真题和解析