Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 10845๋ฒˆ - ํ

๊ฐ์ž ๐Ÿฅ” 2022. 2. 4. 16:28
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๋งํฌ

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

 

10845๋ฒˆ: ํ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

  • ์ด์ „์— ํ’€์—ˆ๋˜ ์Šคํƒ, ์—๋””ํ„ฐ์™€ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ง€๋Š” ์ฝ”๋“œ์ด๋ฏ€๋กœ ๋ณ„ ๊ณ ๋ฏผ์—†์ด ๋ฐ”๋กœ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
import Foundation

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


for _ in 0..<n {
    var order = readLine()!.split(separator: " ").map { String($0) }
    switch order[0] {
    case "push":
        answer.append(order.last!)
    case "pop":
        if answer.isEmpty {
            print(-1)
        } else{
            print(answer.removeFirst())
        }
    case "size":
        print(answer.count)
    case "empty":
        if answer.isEmpty {
            print(1)
        }else{
            print(0)
        }
    case "front":
        if answer.isEmpty {
            print(-1)
        }else{
            print(answer[0])
        }
    default :
        if answer.isEmpty {
            print(-1)
        }else{
            print(answer.last!)
        }
    }
}
๋ฐ˜์‘ํ˜•