首页 > 其他 > 详细

LC 1381. Design a Stack With Increment Operation

时间:2020-03-15 20:56:56      阅读:63      评论:0      收藏:0      [点我收藏+]

link

技术分享图片

 

 

class CustomStack {
public:
    vector<int> stk, inc;
    int msize;
    CustomStack(int maxSize) {
        msize=maxSize;
        inc.resize(msize);
    }
    
    void push(int x) {
        if(stk.size()<msize){
            stk.push_back(x);
        }
    }
    
    int pop() {
        int last=stk.size()-1;
        if(last<0) return -1;
        int res=stk.back()+inc[last];
        stk.pop_back();
        if(last>0) inc[last-1]+=inc[last];
        inc[last]=0;
        return res;
    }
    
    void increment(int k, int val) {
        int i=min(k,(int)stk.size())-1;
        if(i>=0){
            inc[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);
 */

 

LC 1381. Design a Stack With Increment Operation

原文:https://www.cnblogs.com/FEIIEF/p/12499492.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!