=> 스택은 깊이 우선 탐색(DFS)와 백트래킹 종류의 코딩 테스트에 효과적

=> 큐는 너비 우선 탐색(BFS)에서 자주 사용함
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int num = 1;
Stack<Integer> stack = new Stack<>();
StringBuffer bf = new StringBuffer();
boolean result = true;
for(int i=0; i<n ;i++){
arr[i]=sc.nextInt();
}
for(int i=0; i<n ;i++){
if(arr[i]>=num){
while(arr[i]>=num){
stack.push(num++);
bf.append("+\n");
}
stack.pop();
bf.append("-\n");
}else{
int popedNum= stack.pop();
if(popedNum>arr[i]){
System.out.println("NO");
result = false;
break;
}else{
bf.append("-\n");
}
}
}
if(result==true){
System.out.println(bf.toString());
}
}
}
얻어갈 점:
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Queue<Integer> queue = new LinkedList<>();
for(int i=0 ; i<n ; i++){
queue.add(i+1);
}
while(queue.size()>1){
queue.remove();
int polledData= queue.poll();
queue.add(polledData);
}
System.out.print(queue.poll());
}
}
얻어갈 점: