题解 | #【模板】栈#

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

  • 注意输入是一行一行input
class stack(object):
    def __init__(self):
        self.stack = list()
        self.p = 0

    def push(self, num):
        self.stack.append(num)
        self.p += 1

    def top(self):
        if self.p > 0:
            print(self.stack[-1])
            return
        print('error')  

    def pop(self):
        if self.p > 0:
            self.p -= 1
            res = self.stack.pop()
            print(res)
            return
        print('error')

    def check(self, x):
        if work == 'pop':
            self.pop()
        elif work == 'top':
            self.top()
        else:
            self.push(int(work.split()[-1]))

n = input()
tmp = stack()
for i in range(int(n)):
    work = input()
    tmp.check(work)
全部评论

相关推荐

在喝茶的牛油很喜欢吃...:今天oc了
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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