10866번 : 덱 - Python

Pobi·2023년 1월 23일
0

PS

목록 보기
25/107

문제

https://www.acmicpc.net/problem/10866

풀이

python에는 collection이라는 모듈에 deque라는 클래스로 이미 구현되어 있다. 이를 이용해보자.

코드

from collections import deque
from sys import stdin

input = stdin.readline

N = int(input())

deq = deque()

for i in range(N):
    S = input()
    
    if 'push_front' in S:
        S, T = S.split()
        T = int(T)
        deq.appendleft(T)
    elif 'push_back' in S:
        S, T = S.split()
        T = int(T)
        deq.append(T)
    elif 'pop_front' in S:
        if not deq:
            print('-1')
        else:
            print(deq.popleft())
    elif 'pop_back' in S:
        if not deq:
            print('-1')
        else:
            print(deq.pop())
    elif 'size' in S:
        print(len(deq))
    elif 'empty' in S:
        if not deq:
            print('1')
        else:
            print('0')
    elif 'front' in S:
        if not deq:
            print('-1')
        else:
            print(deq[0])
    else:
        if not deq:
            print('-1')
        else:
            print(deq[-1])
profile
꿈 많은 개발자

0개의 댓글