题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
#include <vector> class Solution { public: void push(int value) { stack.push_back(value); } void pop() { if (!stack.empty()) { stack.erase(stack.end()-1); } } int top() { if (!stack.empty()) { return stack.back(); } return 0; } int min() { int result = stack.front(); for (int i = 0; i < stack.size(); i++) { if (result > stack[i]) { result = stack[i]; } } return result; } private: std::vector<int> stack; };