๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/10845
๋ด๊ฐ ํผ ํ์ด
- ์ด์ ์ ํ์๋ ์คํ, ์๋ํฐ์ ๋น์ทํ ๊ตฌ์กฐ๋ก ์ด๋ฃจ์ด์ง๋ ์ฝ๋์ด๋ฏ๋ก ๋ณ ๊ณ ๋ฏผ์์ด ๋ฐ๋ก ํด๊ฒฐ ํ ์ ์์๋ค.
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!)
}
}
}
๋ฐ์ํ
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] (Swift) 10866๋ฒ - ๋ฑ(deque) (0) | 2022.02.06 |
---|---|
[๋ฐฑ์ค] (Swift) 1158๋ฒ - ์์ธํธ์ค ๋ฌธ์ (0) | 2022.02.05 |
[๋ฐฑ์ค] (Swift) 1406๋ฒ - ์๋ํฐ (2) | 2022.01.30 |
[๋ฐฑ์ค] (Swift) 1874๋ฒ - ์คํ ์์ด (0) | 2022.01.28 |
[๋ฐฑ์ค] (Swift) 9012๋ฒ - ๊ดํธ (0) | 2022.01.19 |