今天做了一道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++里面都有已经有了现成的代码!!!多学习!!!

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2021-12-20
  • 2021-09-23
  • 2021-11-20
  • 2021-07-09
猜你喜欢
  • 2021-05-19
  • 2021-09-13
  • 2022-03-06
  • 2022-01-07
  • 2022-03-07
  • 2021-10-30
相关资源
相似解决方案