2024每日刷题(130)
Leetcode—155. 最小栈
实现代码
class MinStack {
public:MinStack() {}void push(int val) {if(st.empty()) {st.emplace(val, val);} else {st.emplace(val, min(val, st.top().second));}}void pop() {if(st.empty()) {return;} else {st.pop();}}int top() {if(st.empty()) {return -1;} else {return st.top().first;} }int getMin() {if(st.empty()) {return -1;} else {return st.top().second;}}
private:stack<pair<int, int>> st;
};/*** Your MinStack object will be instantiated and called as such:* MinStack* obj = new MinStack();* obj->push(val);* obj->pop();* int param_3 = obj->top();* int param_4 = obj->getMin();*/
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!