[leetcode]5357. 設計一個支持增量操作的棧

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

class CustomStack {
    vector<int>stk;
    int maxSize;
    int top;
public:
    CustomStack(int m) {
        stk = vector<int>(m);
        maxSize = m;
        top = -1;
    }
    
    void push(int x) {
        if(top == maxSize-1)
        {
            return ;
        }
        stk[++top] = x;
    }
    
    int pop() {
        if(top == -1)
        {
            return -1;
        }
        return stk[top--];
    }
    
    void increment(int k, int val) {
        for(int i = 0 ; i <= top && i < k; i++)
        {
            stk[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);
 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章