LeetCode 155. Min Stack

来源:互联网 发布:对讲机写频软件 编辑:程序博客网 时间:2024/06/06 06:50

描述

实现栈

解决

两个栈来模拟


class MinStack {public:    stack<int> s1, s2;    MinStack() {    }    void push(int x) {        s1.push(x);        if (s2.empty() || x <= s2.top())            s2.push(x);    }    void pop() {        int temp = s1.top();        s1.pop();        if (temp == s2.top())            s2.pop();    }    int top() {        return s1.top();    }    int getMin() {        return s2.top();    }};
0 0