151. 스택

아현·2021년 7월 7일
0

Algorithm

목록 보기
152/400
post-custom-banner

백준


1. Python

정상 작동


import sys
n = int(sys.stdin.readline())

stack=[]
for i in range(n):
    command = 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])
            

런타임 에러


import sys
input = sys.stdin.readline
n = int(input())
stack = []
for _ in range(n):

  com = input().split()

  if com[0] == "push":
    stack.push(int(com[1]))
  elif com[0] == "pop":
    if len(stack)==0:
        print(-1)
    else:
        print(stack.pop())
  elif com[0] == "size":
    print(len(stack))
  elif com[0] == "empty":
    if len(stack) == 0:
      print(1)
    else:
      print(0)
  elif com[0] == "top":
    if len(stack) == 0:
      print(-1)
    else: 
      print(stack[-1])




profile
For the sake of someone who studies computer science
post-custom-banner

0개의 댓글