网站被主流搜索引擎收录的网页数量是多少,单机游戏,常州优化网站,百度一下就知道手机版LeetCode LCR147.最小栈 思路#x1f914;#xff1a; 建立两个栈#xff0c;一个栈正常入栈出栈#xff0c;一个栈只用于出入最小数#xff0c;当push值小于minst栈顶才入栈#xff0c;当pop值等于minst栈顶才出栈。 代码#x1f50e;#xff1a; class MinStack {
pu…LeetCode LCR147.最小栈 思路 建立两个栈一个栈正常入栈出栈一个栈只用于出入最小数当push值小于minst栈顶才入栈当pop值等于minst栈顶才出栈。 代码 class MinStack {
public:MinStack() {//自定义类型编译器会去调用相关构造}void push(int x) {_st.push(x);if(_minst.empty() || x _minst.top()) //如果更小就push如果最小栈为空就先入一个{_minst.push(x);}}void pop() {if(_st.top() _minst.top()) //出栈值跟最小栈元素一样才一起出{_minst.pop();}_st.pop();}int top() {return _st.top(); //正常栈返回即可}int getMin() {return _minst.top(); //最小栈返回}stackint _st;stackint _minst;
};