백준 10828번 "스택"

sanha_OvO·2021년 4월 21일
0

Algorithm

목록 보기
25/84

문제

백준 10828번 스택


풀이

자료구조 '스택'의 구현 문제
파이썬 리스트를 이용하여 구현하였다.


Python 코드

import sys
input = sys.stdin.readline

stack = []
for _ in range(int(input())):
  s = input().split()
  if s[0] == 'push':
    stack.append(int(s[1]))
  if s[0] == 'pop':
    if not stack:
      print(-1)
    else:
      print(stack.pop(len(stack)-1))
  if s[0] == 'size':
    print(len(stack))
  if s[0] == 'empty':
    if not stack:
      print(1)
    else:
      print(0)
  if s[0] == 'top':
    if not stack:
      print(-1)
    else:
      print(stack[len(stack)-1])
profile
Web Developer / Composer

0개의 댓글