标签:space minstack std class using color else pac empty
#include <iostream> #include <stack> using namespace std; class GetMinStack{ public: void push(int x); void pop(); int top(); int getmin(); private: stack<int> S; stack<int> temp; }; void GetMinStack::push(int x) { if(S.empty()) { temp.push(x); S.push(x); } else { if(x < temp.top()) { temp.push(x); S.push(x); } else { temp.push(temp.top()); S.push(x); } } } void GetMinStack::pop() { S.pop(); temp.pop(); } int GetMinStack::top() { return S.top(); } int GetMinStack::getmin() { return temp.top(); } int main() { GetMinStack TestStack; TestStack.push(-2); cout << TestStack.top() << endl; cout << TestStack.getmin() << endl; TestStack.push(0); cout << TestStack.getmin() << endl; TestStack.push(-6); cout << TestStack.getmin() << endl; TestStack.pop(); cout << TestStack.getmin() << endl; return 0; }
标签:space minstack std class using color else pac empty
原文地址:https://www.cnblogs.com/11ys/p/14266531.html