티스토리 뷰
10866번: 덱
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
문제 요약
- 덱 (Deque) 만들기
풀이
import Foundation
let N = Int(readLine()!)!
var deque: [Int] = []
func push_front(_ x: Int) {
deque.insert(x, at: 0)
}
func push_back(_ x: Int) {
deque.append(x)
}
func pop_front() -> Int {
return deque.isEmpty ? -1 : deque.removeFirst()
}
func pop_back() -> Int {
return deque.isEmpty ? -1 : deque.removeLast()
}
func size() -> Int {
return deque.count
}
func empty() -> Int {
return deque.isEmpty ? 1 : 0
}
func front() -> Int {
return deque.isEmpty ? -1 : deque.first!
}
func back() -> Int {
return deque.isEmpty ? -1 : deque.last!
}
for _ in 1...N {
let command = readLine()!.components(separatedBy: " ")
switch command[0] {
case "push_front":
push_front(Int(command[1])!)
case "push_back":
push_back(Int(command[1])!)
case "pop_front":
print(pop_front())
case "pop_back":
print(pop_back())
case "size":
print(size())
case "empty":
print(empty())
case "front":
print(front())
case "back":
print(back())
default:
break
}
}
'Algorithm > 백준' 카테고리의 다른 글
[BOJ] (Swift) 10799 - 쇠막대기 (0) | 2022.08.03 |
---|---|
[BOJ] (Swift) 17413 - 단어 뒤집기 2 (0) | 2022.08.03 |
[BOJ] (Swift) 1158 - 요세푸스 문제 (0) | 2022.08.02 |
[BOJ] (Swift) 10845 - 큐 (0) | 2022.08.01 |
[BOJ] (Swift) 1406 - 에디터 (0) | 2022.08.01 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 요시푸스
- BOJ 17298
- BOJ 10866
- crud
- git
- github
- 2023 회고
- Card CollectionVIew
- 1406 에디터
- UICollectionView Error
- Carousel Effect
- BOJ 10809
- BOJ 10799
- Custom Detent
- BOJ 10808
- BOJ 17413
- sheetPresentationController
- Algorithm
- NaverMaps
- ios
- autolayout
- bottom sheet
- Sheet Height
- BOJ 10820
- swift
- Stack
- Gradient View
- CAGradientLayer
- UICollectionView banner
- BOJ 1935
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함