ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [java] 백준 알고리즘 10866번 덱 풀이
    알고리즘/백준 알고리즘 2018. 8. 6. 14:22



    덱은 스택과 큐의 특징을 골고루 가지고 있다.


    First In, First Out도 가능하고, First In, Last Out도 가능하다.




    양방향으로 이동이 가능하다.


    자바에서 덱을 제공해준다. 덱을 활용해 문제 해결.



    * 풀이 소스


    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
     
    public class Baekjoon10866{
     
        static StringTokenizer st;
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            st = new StringTokenizer(br.readLine());
            StringBuilder sb = new StringBuilder();
            Deque<Integer> deque = new ArrayDeque<>();
            
            int orderCount = Integer.parseInt(st.nextToken());
            
            for(int i=0; i<orderCount; i++) {
                st = new StringTokenizer(br.readLine());
                String order = st.nextToken();
                
                if(order.equals("push_front")) {
                    
                    deque.addFirst(Integer.parseInt(st.nextToken()));
                    
                }else if(order.equals("push_back")) {
                    
                    deque.addLast(Integer.parseInt(st.nextToken()));
                    
                }else if(order.equals("pop_front")){
                    
                    if(deque.isEmpty()) {
                        sb.append("-1\n");
                    }else {
                        sb.append(deque.pollFirst()+"\n");
                    }
                    
                }else if(order.equals("pop_back")) {
                    
                    if(deque.isEmpty()) {
                        sb.append("-1\n");
                    }else {
                        sb.append(deque.pollLast()+"\n");
                    }
                    
                }else if(order.equals("size")) {
                    
                    sb.append(deque.size()+"\n");
                    
                }else if(order.equals("empty")) {
                    
                    if(deque.isEmpty()) {
                        sb.append("1\n");
                    }else {
                        sb.append("0\n");
                    }
                    
                }else if(order.equals("front")) {
                    
                    if(deque.isEmpty()) {
                        sb.append("-1\n");
                    }else {
                        sb.append(deque.getFirst()+"\n");
                    }
                    
                }else if(order.equals("back")) {
                    
                    if(deque.isEmpty()) {
                        sb.append("-1\n");
                    }else {
                        sb.append(deque.getLast()+"\n");
                    }
                }
            }
            
            bw.write(sb.toString());
            bw.flush();
        }
     
    }
     
    cs


Designed by Tistory.