最小棧

使用2個棧實現最小棧

class Solution {
    private:
    stack<int> s1;
    stack<int> s2;
public:
    void push(int value) {
        s1.push(value);
        if(s2.empty()==1 || value <= s2.top())
        {
            s2.push(value);
        }
    }
    void pop() {
        if(s1.empty()==1)
            return ;
        if(s1.top()==s2.top())
            s2.pop();
        s1.pop();
    }
    int top() {
        return s1.top();
    }
    int min() {
        return s2.top();
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章