[백준] 10828번 : 스택 - Python(파이썬)

강재원·2022년 11월 2일
0

[코딩테스트] Python

목록 보기
168/200
post-custom-banner



https://www.acmicpc.net/problem/10828

class Stack:
    def __init__(self):
        self.si = 0
        self.stack = []

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

    def pop(self):
        if self.si == 0:
            return -1
        else:
            ans = self.stack[self.si - 1]
            del self.stack[self.si - 1]
            self.si -= 1
            return ans

    def size(self):
        return self.si

    def empty(self):
        if self.si == 0:
            return 1
        else:
            return 0

    def top(self):
        if self.si == 0:
            return -1
        else:
            return self.stack[-1]


n=int(input())
t=Stack()
an=[]
count=0
for i in range(n):
    arr = input().split()
    st = arr[0]
    if st == "push":
        t.push(arr[1])
    elif st == "pop":
        an.append(t.pop())
        count+=1
    elif st == "size":
        an.append(t.size())
        count+=1
    elif st == "empty":
        an.append(t.empty())
        count+=1
    elif st == "top":
        an.append(t.top())
        count+=1

for i in range(count):
    print(an[i])
profile
개념정리 & 문법 정리 & 알고리즘 공부
post-custom-banner

0개의 댓글