[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();
    }
};

  

posted @ 2014-12-31 23:11  阿牧遥  阅读(149)  评论(0编辑  收藏  举报