leetcode 155. Min Stack
Posted StrongYaYa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 155. Min Stack相关的知识,希望对你有一定的参考价值。
题目描述:
class MinStack { public: /** initialize your data structure here. */ MinStack() { } void push(int x) { data.push(x); if(min_data.empty()){ min_data.push(x); } else{ if(min_data.top() > x) min_data.push(x); else min_data.push(min_data.top()); } } void pop() { data.pop(); min_data.pop(); } int top() { return data.top(); } int getMin() { return min_data.top(); } private: stack<int> data; stack<int> min_data; }; /** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
以上是关于leetcode 155. Min Stack的主要内容,如果未能解决你的问题,请参考以下文章