码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]Min Stack

时间:2015-01-01 00:04:51      阅读:179      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!