1381设计一个支持增量操作的栈 Posted on 2020-05-11 Words count in article: 128 | Reading time ≈ 1 1381设计一个支持增量操作的栈 下面是解法123456789101112131415161718192021222324252627282930313233class CustomStack {private: vector<int>v; int top;public: CustomStack(int maxSize) { v.resize(maxSize); top = -1; } void push(int x) { if(top!=v.size()-1){ ++top; v[top] = x; } } //利用vector模仿栈,这里的pop不是真的pop,原来的数据还保留,只是top变了 int pop() { if(top==-1){ return -1; } --top; return v[1+top]; } void increment(int k, int val) { int temp = min(k,top+1); for(int i = 0;i<temp;i++) { v[i] +=val; } }}; -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/05/11/1381%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%94%AF%E6%8C%81%E5%A2%9E%E9%87%8F%E6%93%8D%E4%BD%9C%E7%9A%84%E6%A0%88/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.