iOS/코딩테스트

백준 10828번 스택(swift)

개발자 Leo 2022. 8. 22. 14:50
import Foundation

let N = Int(readLine()!)
var stack = Array<Int>()

for _ in 0..<N! {
    let input = readLine()!.split(separator: " ").map(){ String($0) }
    switch (input[0]) {
    case "push":
        stack.append(Int(input[1])!)
    case "pop":
        if let last = stack.last{
            print("\(last)")
            stack.removeLast()
        } else {
            print("-1")
        }
    case "top":
        if stack.isEmpty {
            print("-1")
        } else {
            print("\(stack.last!)")
        }
    case "size":
        print("\(stack.count)")
    case "empty":
        if stack.isEmpty {
            print("1")
        } else {
            print("0")
        }
    default:
        break
    }
}

'iOS > 코딩테스트' 카테고리의 다른 글

백준 9012 (swift)  (0) 2022.08.25
백준 9093 단어뒤집기(swift)  (0) 2022.08.23
백준 온라인 추천 문제  (0) 2022.08.22