热题100 152|155
作者:互联网
155. 最小栈
class MinStack{
stack<int> x_stack;
stack<int> min_stack;
public:
MinStack(){ //构造函数
min_stack.push(INT_MAX);
}
void push(int x){ //想法的关键
x_stack.push(x);
min_stack.push(min(min_stack.top(), x));
}
void pop(){
x_stack.pop();
min_stack.pop();
}
int top(){
return x_stack.top();
}
int getMin(){
return min_stack.top();
}
};
面试时还有不能用额外空间的要求
152. 乘积最大子数组
class Solution{
public:
int maxProduct(vector<int>& nums){
int maxP = nums[0], minF = nums[0], ans = nums[0];
for(int i = 1; i < nums.size(); ++i){
标签:152,155,min,int,top,nums,push,100,stack 来源: https://blog.csdn.net/qq_41762249/article/details/121859201