📁 코딩테스트 준비/Python

[자료구조/python]백준 10828번 스택

박개봄 2023. 4. 30. 15:31
728x90
import sys
n=int(sys.stdin.readline()) #시간초과때문에 sys import해서 입력받음

stack=[]
command=[]

for i in range(n):
  command=list(sys.stdin.readline().split())
  if command[0]=='push':
    stack.append(command[1])
  elif command[0]=='pop':
    if len(stack)==0:
      print(-1)
    else:
      print(stack.pop())
  elif command[0]=='size':
    print(len(stack))
  elif command[0]=='empty':
    if len(stack)==0:
      print(1)
    else:
      print(0)
  elif command[0]=='top':
    if len(stack)==0:
      print(-1)
    else:
      print(stack[-1]) #리스트의 맨 마지막 원소=list[-1]

 

728x90