getmin栈_python3

设计getMin功能的栈

http://www.nowcoder.com/questionTerminal/05e57ce2cd8e4a1eae8c3b0a7e9886be

不需要弄两个列表啊,一个就可以了

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())
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:58
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务