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