TreeviewCopyright © aleen42 all right reserved, powered by aleen42
1381. 设计一个支持增量操作的栈
https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
Java
/*
* @Author: Goog Tech
* @Date: 2020-09-02 21:59:27
* @LastEditTime: 2020-09-02 21:59:46
* @Description: https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
* @FilePath: \leetcode-googtech\#1381. Design a Stack With Increment Operation\Solution.java
* @WebSite: https://algorithm.show/
*/
// 使用数组模拟栈结构
class CustomStack {
int[] stack;
int index;
public CustomStack(int maxSize) {
stack = new int[maxSize];
index = -1;
}
public void push(int x) {
if(index == stack.length - 1) return;
stack[++index] = x;
}
public int pop() {
return index == -1 ? -1 : stack[index--];
}
public void increment(int k, int val) {
for(int i = 0; i < Math.min(k, index + 1); i++) stack[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);
*/
Python
'''
Author: Goog Tech
Date: 2020-09-02 21:59:31
LastEditTime: 2020-09-02 22:00:04
Description: https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
FilePath: \leetcode-googtech\#1381. Design a Stack With Increment Operation\Solution.py
WebSite: https://algorithm.show/
'''
class CustomStack(object):
def __init__(self, maxSize):
"""
:type maxSize: int
"""
self.stack = [] * maxSize
self.maxSize = maxSize
def push(self, x):
"""
:type x: int
:rtype: None
"""
if len(self.stack) < self.maxSize:
self.stack.append(x)
def pop(self):
"""
:rtype: int
"""
if self.stack:
return self.stack.pop()
else:
return -1
def increment(self, k, val):
"""
:type k: int
:type val: int
:rtype: None
"""
for i in range(min(k, len(self.stack))):
self.stack[i] += val
# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)