[백준 10866번 / Java] 덱코딩테스트/백준2023. 7. 1. 08:45
Table of Contents
728x90
728x90
문제 링크
https://www.acmicpc.net/problem/10866
풀이
데크 포스팅 후 링크
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class BaekJoon10866 {
static List<Integer> deque = new ArrayList<>();
static StringBuffer sb = new StringBuffer();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
switch (st.nextToken()) {
case "push_front":
push_front(Integer.parseInt(st.nextToken()));
break;
case "push_back":
push_back(Integer.parseInt(st.nextToken()));
break;
case "pop_front":
pop_front();
break;
case "pop_back":
pop_back();
break;
case "size":
size();
break;
case "empty":
empty();
break;
case "front":
front();
break;
case "back":
back();
break;
}
}
System.out.println(sb.toString());
}
private static void push_front(int s) {
List<Integer> clone = new ArrayList<>();
clone.add(s);
for(int i : deque) clone.add(i);
deque = clone;
}
private static void push_back(int s) {
deque.add(s);
}
private static void pop_front() {
if(deque.isEmpty()) sb.append(-1 + "\n");
else {
sb.append(deque.remove(0) + "\n");
}
}
private static void pop_back() {
if(deque.isEmpty()) sb.append(-1 + "\n");
else {
sb.append(deque.remove(deque.size() - 1) + "\n");
}
}
private static void size() {
sb.append(deque.size() + "\n");
}
private static void empty() {
if(deque.isEmpty()) sb.append(1 + "\n");
else sb.append(0 + "\n");
}
private static void front() {
if(deque.isEmpty()) sb.append(-1 + "\n");
else {
sb.append(deque.get(0) + "\n");
}
}
private static void back() {
if(deque.isEmpty()) sb.append(-1 + "\n");
else {
sb.append(deque.get(deque.size() - 1) + "\n");
}
}
}
728x90
300x250
@mag1c :: 꾸준히 재밌게
2023.04 ~ 백엔드 개발자의 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!