class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.stack = [] # 存放所有元素 self.minStack = [] # 存放每一次压入数据时,栈中的最小值(如果压入数据的值大于栈中的最小值就不需要重复压入最小值,小于或者等于栈中最小值则需要压入) def push(self, x): """ :type x: int :rtype: void """ self.stack.append(x) if not self.minStack or self.minStack[-1] >= x: self.minStack.append(x) def pop(self): # 移除栈顶元素时,判断是否移除栈中最小值 """ :rtype: void """ if self.minStack[-1] == self.stack[-1]: del self.minStack[-1] self.stack.pop() def top(self): """ :rtype: int """ return self.stack[-1] def getMin(self): """ :rtype: int """ return self.minStack[-1] if __name__ == '__main__': m = MinStack() m.push(2) m.push(3) m.push(1) m.push(-1) # m.push(5) m.pop() print(m.stack) print(m.minStack)