Algorithm/baekjoon

마닐라 2022. 1. 29. 23:24

📍 문제 설명

 

💡 접근

덱 기초문제

 

👩‍💻 코드

import java.util.*;

public class Main {
    static int n;

    public static void main(String[] args) {
        Main T = new Main();
        Scanner kb = new Scanner(System.in);

        n = kb.nextInt();
        kb.nextLine();
        Deque<Integer> dq = new ArrayDeque<>();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++) {
            String[] s = kb.nextLine().split(" ");
            switch (s[0]) {
                case "push_front" : {
                    dq.addFirst(Integer.parseInt(s[1]));
                    break;
                }
                case "push_back" : {
                    dq.addLast(Integer.parseInt(s[1]));
                    break;
                }
                case "pop_front" : {
                    if(!dq.isEmpty()) sb.append(dq.pollFirst()).append("\n");
                    else sb.append(-1).append("\n");
                    break;
                }
                case "pop_back" : {
                    if(!dq.isEmpty()) sb.append(dq.pollLast()).append("\n");
                    else sb.append(-1).append("\n");
                    break;
                }
                case "size" : {
                    sb.append(dq.size()).append("\n");
                    break;
                }
                case "empty" : {
                    if(dq.isEmpty()) sb.append(1).append("\n");
                    else sb.append(0).append("\n");
                    break;
                }
                case "front" : {
                    if(!dq.isEmpty()) sb.append(dq.peekFirst()).append("\n");
                    else sb.append(-1).append("\n");
                    break;
                }
                case "back" : {
                    if(!dq.isEmpty()) sb.append(dq.peekLast()).append("\n");
                    else sb.append(-1).append("\n");
                    break;
                }
            }
        }
        System.out.println(sb);

    }

    private void solution() {

    }
}

'Algorithm > baekjoon' 카테고리의 다른 글

히스토그램==========  (0) 2022.01.30
  (0) 2022.01.30
문자열 폭발  (0) 2022.01.28
크게 만들기  (0) 2022.01.28
쇠막대기  (0) 2022.01.27