๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/10828
๋ฐ์ํ
๋ด๊ฐ ํผ ํ์ด
์ผ๋ฐ์ ์ธ ์คํ์ ๊ฐ๋ ์ ํ์ธํ๋ ๋ฌธ์ ๋ค. ํจ์๋ก ๊ฐ ๊ธฐ๋ฅ์ ๊ตฌํํ์๊ณ , switch case๋ฌธ์ ํ์ฉํ์๋ค.
var stack:[Int] = []
let n = Int(readLine()!)!
for _ in 1...n{
let a = readLine()!.split(separator:" ").map{String($0)}
switch a[0] {
case "push":
push(Int(a[1])!)
case "pop":
print(pop())
break
case "size":
print(size())
break
case "empty":
print(empty())
break
case "top":
print(top())
break
default:
break
}
}
func push(_ x:Int){
stack.append(x)
}
func pop() -> Int{
if let popValue = stack.popLast() {
return popValue
} else {
return -1
}
}
func size() -> Int{
return stack.count
}
func empty() -> Int{
if stack.isEmpty {
return 1
}else{
return 0
}
}
func top() -> Int{
if let last = stack.last {
return last
}else{
return -1
}
}
๋ฐ์ํ
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] (Swift) 9012๋ฒ - ๊ดํธ (0) | 2022.01.19 |
---|---|
[๋ฐฑ์ค] (Swift) 9093๋ฒ - ๋จ์ด ๋ค์ง๊ธฐ (0) | 2022.01.16 |
[๋ฐฑ์ค] 2178๋ฒ - ๋ฏธ๋กํ์ (ํ์ด์ฌ) (0) | 2021.10.14 |
[๋ฐฑ์ค] 1260๋ฒ - DFS์ BFS (ํ์ด์ฌ) (0) | 2021.10.14 |
[python3] ์ด์ฝํ - ๋ฌธ์์ด ์ฌ์ ๋ ฌ (ch.12 ๊ตฌํ - ์ ํ๋ณ ๊ธฐ์ถ๋ฌธ์ ) (0) | 2021.06.23 |