class CustomStack {
public:
int top=-1;
vector<int>obj;
CustomStack(int maxSize) {
obj.resize(maxSize);
}
void push(int x) {
if(top+1!=obj.size())//这里要加上一个判断,不然会导致越界
obj[++top]=x;
}
int pop() {
if(top==-1)
{
return -1;
}
int temp=obj[top];
top--;
return temp;
}
void increment(int k, int val) {
for(int i=0;i<min(k,top+1);i++)
{
obj[i]+=val;
}
}
};
/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack* obj = new CustomStack(maxSize);
* obj->push(x);
* int param_2 = obj->pop();
* obj->increment(k,val);
*/
LeetCode:1381. 设计一个支持增量操作的栈
最新推荐文章于 2022-07-09 09:40:03 发布