题目描述

定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
class Solution {
public:
    void push(int value) {
        st.push(value);
    }
    void pop() {
        st.pop();
    }
    int top() {
        return st.top();
    }
    int min() {
        int min_num=st.top();
        while(!st.empty()){
            int tmp = st.top();
            if(tmp < min_num){
                min_num = tmp;
            }
            tmp_st.push(tmp);
            pop();
        }
        while(!tmp_st.empty()){
            st.push(tmp_st.top());
            tmp_st.pop();
        }
        return min_num;
    }
    private: 
    stack<int>st;
    stack<int>tmp_st;
};

  

相关文章:

  • 2022-01-11
  • 2021-07-01
  • 2021-08-26
  • 2021-06-10
  • 2021-12-12
  • 2021-11-05
  • 2021-08-29
  • 2021-07-23
猜你喜欢
  • 2021-12-10
  • 2021-12-15
  • 2021-08-23
  • 2021-07-12
  • 2021-09-26
  • 2021-12-02
  • 2021-08-19
相关资源
相似解决方案