155. Min Stack 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021222324252627282930class MinStack {private: stack<int> sta, minsta; public: MinStack() { } void push(int val) { sta.push(val); // 單調遞減stack if(minsta.empty()) minsta.push(val); else if(!minsta.empty() && minsta.top()>=val ) minsta.push(val); } void pop() { int t = sta.top(); sta.pop(); if(t == minsta.top()) minsta.pop(); } int top() { return sta.top(); } int getMin() { return minsta.top(); }};