https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Deque<Integer> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
String[] word = br.readLine().split(" ");
switch (word[0]) {
case "push":
queue.offer(Integer.parseInt(word[1]));
break;
case "pop":
if (!queue.isEmpty()) System.out.println(queue.poll());
else System.out.println(-1);
break;
case "size":
System.out.println(queue.size());
break;
case "empty":
System.out.println(queue.isEmpty() ? 1 : 0);
break;
case "front":
if (!queue.isEmpty()) System.out.println(queue.peek());
else System.out.println(-1);
break;
case "back":
if (!queue.isEmpty()) System.out.println(queue.peekLast());
else System.out.println(-1);
break;
}
}
}
}
깨달은 점:
push할 때 그 수를 변수에 저장해놓으면 굳이 deque를 사용하지 않고 queue로 구현이 가능.
switch case문은 case가 끝날 때 break를 하지 않으면 나머지 case를 다 돌게됨.
'Java > AlgorithmPS' 카테고리의 다른 글
프로그래머스) 기능개발 (0) | 2023.10.14 |
---|---|
백준) 카드2(2164 번) (0) | 2023.10.12 |
프로그래머스) 올바른 괄호 (0) | 2023.10.05 |
백준) 균형잡힌 세상 (4949번) (0) | 2023.10.04 |
백준) 도키도키 간식드리미 (12789번) (0) | 2023.09.27 |