题解 | 包含min函数的栈
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
class Solution { private: stack<int> st; stack<int> mst; public: void push(int value) { st.push(value); if(mst.empty()) { mst.push(value); return; } int cur = mst.top(); if(value <= cur) mst.push(value); } void pop() { if (st.empty()) return; int tp = st.top(); st.pop(); if(tp == mst.top()) mst.pop(); } int top() { return st.top(); } int min() { return mst.top(); } };