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!)
}
}
}
๋ฐ์ํ