题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
class Solution { public: void push(int value) { stack1.push(value); if(!stack2.empty()){ stack2.push(std::min(value,stack2.top())); }else{ stack2.push(value); } } void pop() { if(!stack1.empty()){ stack1.pop(); stack2.pop(); } } int top() { if(!stack1.empty()) return stack1.top(); else return -1; } int min() { if(!stack1.empty()) return stack2.top(); else return -1; } private: stack<int> stack1; stack<int> stack2; };