sm 기술 블로그
161. 10866(덱) - 자바 본문
import java.util.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
Deque<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < N; i++) {
String[] tmp = br.readLine().split(" ");
String S;
int num = 0;
if(tmp.length == 2) {
S = tmp[0];
num = Integer.parseInt(tmp[1]);
}
else {
S = tmp[0];
}
if(S.equals("push_back")) queue.addLast(num);
else if(S.equals("push_front")) queue.addFirst(num);
else if(S.equals("pop_front")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.pollFirst()).append("\n");
}
else if(S.equals("pop_back")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.pollLast()).append("\n");
}
else if(S.equals("size")) sb.append(queue.size()).append("\n");
else if(S.equals("empty")) {
if(queue.isEmpty()) sb.append(1).append("\n");
else sb.append(0).append("\n");
}
else if(S.equals("front")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekFirst()).append("\n");
}
else if(S.equals("back")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekLast()).append("\n");
}
}
System.out.print(sb);
}
}
문제요약
- push_front X: 정수 X를 덱의 앞에 넣는다.
- push_back X: 정수 X를 덱의 뒤에 넣는다.
- pop_front: 덱의 가장 앞에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- pop_back: 덱의 가장 뒤에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- size: 덱에 들어있는 정수의 개수를 출력한다.
- empty: 덱이 비어있으면 1을, 아니면 0을 출력한다.
- front: 덱의 가장 앞에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- back: 덱의 가장 뒤에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
설명
- push_front X: 정수 X를 덱의 앞에 넣는다.
if(S.equals("push_back")) queue.addLast(num);
- push_back X: 정수 X를 덱의 뒤에 넣는다.
else if(S.equals("push_front")) queue.addFirst(num);
- pop_front: 덱의 가장 앞에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
else if(S.equals("pop_front")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.pollFirst()).append("\n");
}
- pop_back: 덱의 가장 뒤에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
else if(S.equals("pop_back")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.pollLast()).append("\n");
}
- size: 덱에 들어있는 정수의 개수를 출력한다.
else if(S.equals("size")) sb.append(queue.size()).append("\n");
- empty: 덱이 비어있으면 1을, 아니면 0을 출력한다.
else if(S.equals("empty")) {
if(queue.isEmpty()) sb.append(1).append("\n");
else sb.append(0).append("\n");
}
- front: 덱의 가장 앞에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
else if(S.equals("front")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekFirst()).append("\n");
}
- back: 덱의 가장 뒤에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
else if(S.equals("back")) {
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekLast()).append("\n");
}
'문제 > 백준_자바' 카테고리의 다른 글
163. 5430(AC) - 자바 (0) | 2022.07.28 |
---|---|
162. 1021(회전하는 큐) - 자바 (0) | 2022.07.27 |
160. 1966(프린트 큐) - 자바 (0) | 2022.07.26 |
159. 11866(요세푸스 문제 0) - 자바 (0) | 2022.07.26 |
158. 2164(카드2) - 자바 (0) | 2022.07.26 |
Comments