백준 28278번 스택2 - python

황서정·2025년 5월 7일

문제 설명



내 코드

import sys
input = sys.stdin.readline

N = int(input())

stack = []
result = []

for _ in range(N):
    cmd = input().strip()
    
    if cmd[0] == '1':
        _, x  = cmd.split()
        stack.append(int(x))
    
    elif cmd == '2':
        if stack:
            result.append(str(stack.pop()))
        else:
            result.append('-1')
    
    elif cmd == '3':
        result.append(str(len(stack)))
    
    elif cmd == '4':
        result.append('0' if stack else '1')
    
    elif cmd == '5':
        if stack:
            result.append(str(stack[-1]))
        else:
            result.append('-1')
print('\n'.join(result))
    
profile
안녕하세요 ❗️풀스택 개발자 및 클라우드 인프라 및 DevOps 엔지니어로 성장하고 있는 황서정입니다.

0개의 댓글