首页 > 其他 > 详细

[leetcode]Min Stack

时间:2015-01-01 00:01:39      阅读:351      评论:0      收藏:0      [点我收藏+]

老题目。两个栈。

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!