首页 > 代码库 > [leetcode]Min Stack

[leetcode]Min Stack

老题目。两个栈。

class MinStack {    stack<int> stk;    stack<int> minstk;public:    void push(int x) {        stk.push(x);        if (minstk.empty() || minstk.top() >= x) {            minstk.push(x);        }    }    void pop() {        int x = stk.top();        stk.pop();        if (x == minstk.top()) {            minstk.pop();        }            }    int top() {        return stk.top();    }    int getMin() {        return minstk.top();    }};

  

[leetcode]Min Stack