[leetcode]Min Stack

时间:2015-01-01 00:01:39   收藏:0   阅读:352

老题目。两个栈。

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

  

原文:http://www.cnblogs.com/lautsie/p/4196787.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!