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