스택은 LIFO(Last In First Out) 후입선출의 자료구조이다.
import java.util.*;
class Solution {
Stack<Character> stack = new Stack<>();
boolean solution(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push('(');
continue;
}
if (stack.empty()) {
return false;
}
stack.pop();
}
if (!stack.empty()) {
return false;
}
return true;
}
}
큐는 스택과 반대인 FIFO(First In First Out) 선입선출의 자료구조이다.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
sb.append("<");
Queue<Integer> q = new LinkedList<>();
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int K = Integer.parseInt(input[1]);
for (int i = 1; i <= N; i++) {
q.offer(i);
}
while (!q.isEmpty()) {
for (int i = 1; i < K; i++) {
q.offer(q.poll());
}
sb.append(q.poll());
if (!q.isEmpty()) {
sb.append(", ");
}
}
sb.append(">");
System.out.println(sb);
}
}