LeetCode 3 :Min Stack

今天做了一道MinStack的题,深深的感到自己C++还完全没有学好!!!

#include <iostream>
#include <stack>
using std::stack;

class MinStack {
public:
    stack<int> st;
    stack<int> stm;
    void push(int x) 
    {
        st.push(x);
        if(stm.empty() || stm.top()>=x)
        {
            stm.push(x);
        }
    }

    void pop() 
    {
        int topElem = st.top();
        st.pop();
        if(topElem <= stm.top())
        {
            stm.pop();
        }

    }

    int top() 
    {
        return st.top();
    }

    int getMin() 
    {
        return stm.top();
    }
};

以上是参考http://www.cnblogs.com/x1957/p/4086448.html他的代码,基本的数据结构在C++里面都有已经有了现成的代码!!!多学习!!!

原文地址:https://www.cnblogs.com/bestwangjie/p/4161513.html