1 class CustomStack: 2 def __init__(self, maxSize: int): 3 self.maxSize = maxSize 4 self.stack = [0] * maxSize 5 self.position = -1 6 7 def push(self, x: int) -> None: 8 if self.position < self.maxSize - 1: 9 self.position += 1 10 self.stack[self.position] = x 11 12 def pop(self) -> int: 13 if self.position >= 0: 14 top = self.stack[self.position] 15 self.position -= 1 16 return top 17 else: 18 return -1 19 20 def increment(self, k: int, val: int) -> None: 21 for i in range(min(k,self.maxSize)): 22 self.stack[i] += val
算法类型:设计题。使用数组实现栈。