본문 바로가기
Algoritm/Quiz-Solutions

[백준 10854] 큐 (JAVA) - Queue

by 💜autumn 2020. 5. 3.

[문제링크]

https://www.acmicpc.net/problem/10845

 

[풀이소스]

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
package algorithm;
 
 
public class Queue10845 {
    public static LinkedList<Integer> queue;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int testcase=Integer.parseInt(br.readLine());
        
        StringTokenizer st=null;
        queue=new LinkedList<Integer>();
        StringBuilder result=new StringBuilder();
        int lastIn=-1;
 
        while(testcase-->0) {
            st=new StringTokenizer(br.readLine());
            String method=st.nextToken();
            
            if (method.equals("push")) {
                lastIn=Integer.parseInt(st.nextToken());
                queue.offer(lastIn);
            } else if (method.equals("pop")) {
                if (queue.isEmpty()) {
                    result.append("-1 \n");
                    lastIn=-1;
                }
                else {
                    result.append(queue.poll()+"\n");
                    if(queue.size()==0) lastIn=-1;
                }
            } else if(method.equals("size")) {
                result.append(queue.size()+"\n");
            } else if(method.equals("empty")) {
                if(queue.isEmpty()) {
                    result.append("1 \n");
                } else {
                    result.append("0 \n");
                }
            } else if(method.equals("front")) {
                if (queue.isEmpty()) result.append("-1 \n");
                else result.append(queue.peek()+"\n");
            } else if(method.equals("back")) {
                if (queue.isEmpty()) result.append("-1 \n");
                else result.append(lastIn+" \n");
            }
        }
        System.out.println(result);
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
 

[느낀점]

  - Queue는 LinkedList활용!

  - 가장 많이 활용하는 변수는 밖에

  - 활용될 변수는 조건문안이 아닌 실행될 곳에!

  - 너무 뺑끌뺑글~~~ 돌려생각하지말기!

'Algoritm > Quiz-Solutions' 카테고리의 다른 글

[Leetcode 1] two Sum  (0) 2020.08.02
[백준 2156] 포도주 시식 (Java) - DP  (0) 2020.05.07
[백준 1260] DFS와 BFS (Java) - DFS,BFS  (0) 2020.05.07
[백준 9012] 괄호(Java) - Stack  (0) 2020.05.04
[백준 15953] 상금헌터 (JAVA)  (0) 2020.05.02

댓글