LeetCode 155. Min Stack

    xiaoxiao2021-03-26  18

    解题思路:利用stack 重写 MinStack()

    class MinStack { public: /** initialize your data structure here. */ stack<int>res; stack<int>min; void push(int x) { res.push(x); if(min.empty()||x<=min.top()) min.push(x); } void pop() { if (!min.empty()) { if (res.top() == min.top()) min.pop(); res.pop(); } } int top() { return res.top(); } int getMin() { return min.top(); } };

    转载请注明原文地址: https://ju.6miu.com/read-660480.html

    最新回复(0)