不需要弄两个列表啊,一个就可以了
class Stack(object):
def __init__(self):
self.stack = []
def push(self, value):
if not self.stack or self.stack[-1] >= value:
self.stack.append(value)
else:
self.stack.append(self.stack[-1])
def pop(self):
if self.stack:
self.stack.pop(-1)
def getMin(self):
if self.stack:
return self.stack[-1]
s = Stack()
n = int(input())
for _ in range(n):
l = input().split()
if l[0] == 'push':
s.push(int(l[1]))
elif l[0] == 'pop':
s.pop()
elif l[0] == 'getMin':
print(s.getMin())
京公网安备 11010502036488号