티스토리 뷰

Algorithm/Baekjoon Online Judge

10828번 '스택'

할루루 2018. 2. 23. 17:51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Foundation
 
let input = Int(readLine()!)!
var stack = [Int]()
var commands = [String]()
for _ in 1...input{
    commands.append(readLine()!)
}
for command in commands{
    if(command.contains("push")){
        let number = Int((command.split(separator: " ").last?.description)!)!
        stack.append(number)
    } else {
        switch(command){
        case "pop":
            if let top = stack.popLast(){
                print(top)
            } else {
                print("-1")
            }
        case "size":
            print(stack.count)
        case "empty":
            if(stack.isEmpty){
                print("1")
            } else {
                print("0")
            }
        case "top":
            if let top = stack.last {
                print(top)
            } else {
                print("-1")
            }
        default:
            break
        }
    }
}
cs




'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글

14645번 '와이버스 부릉부릉'  (0) 2018.02.27
9012번 '괄호'  (0) 2018.02.23
1475번 '방 번호'  (0) 2018.02.23
1157번 '단어 공부'  (0) 2018.02.23
2108번 '통계학'  (0) 2018.02.23
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함