쥐수의 공부노트

백준 10866번 덱 본문

바킹독 알고리즘/덱

백준 10866번 덱

쥐수 2023. 6. 14. 16:06
728x90

 

정답 :

let n = Int(readLine()!)!
var array : [Int] = []

for _ in 0..<n {
    let input = readLine()!.split(separator: " ").map{String($0)}
    
    if input[0] == "push_front" {
        var num = Int(input[1])!
        array.insert(num, at: 0)
    }
    if input[0] == "push_back" {
        var num = Int(input[1])!
        array.append(num)
    }
    if input[0] == "pop_front" {
        if array.isEmpty {
            print(-1)
        } else {
            print(array[0])
            array.remove(at: 0)
        }
    }
    if input[0] == "pop_back" {
        if array.isEmpty {
            print(-1)
        } else {
            print(array[array.count-1])
            array.remove(at: array.count-1)
        }
    }
    if input[0] == "size" {
        print(array.count)
    }
    if input[0] == "empty" {
        if array.isEmpty {
            print(1)
        } else {
            print(0)
        }
    }
    if input[0] == "front" {
        if array.isEmpty {
            print(-1)
        } else {
            print(array[0])
        }
    }
    if input[0] == "back" {
        if array.isEmpty {
            print(-1)
        } else {
            print(array[array.count-1])
        }
    }
}
더보기

TMI : insert를 이용하여 위치를 지정해서 넣는다면 문제 없다!!

728x90