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