[백준] 10828번

그녕·2023년 3월 19일
0

알고리즘 문제 풀이

목록 보기
19/33

백준 10828번

스택

🍀내 풀이🍀

딱히 어려운건 없었는데 top을 할때 젤 위에 있는거를 뽑지말고 출력만 해야하는데 이때 어떻게 해야할지 몰랐다. 그치만 stack=[1,2,3,4,5]라고 하면 stack[0]=1...쭉 이렇게 가기 때문에 제일 위에 있는 5를 빼려고 하면 stack의 거꾸로 해야해서 stack[-1]을 해주면 된다.

🍀내 코드🍀

import sys
input=sys.stdin.readline
N=int(input())
stack=[]

for _ in range(N):
    x=input().split()
    if x[0]== "push":
        stack.append(x[1])
    elif x[0]=="pop":
        if not stack:
            print("-1")
        else:
            print(stack.pop())
    elif x[0]=="top":
        if not stack:
            print("-1")
        else:
            print(stack[-1])
    elif x[0]=="size":
        print(len(stack))
    elif x[0] =="empty":
        if not stack:
            print("1")
        else:
            print("0")

0개의 댓글