import java.util.*;
public class Solution {
public int[] solution(int []arr) {
Queue<Integer> queue = new LinkedList<Integer>();
for(int i=0; i < arr.length-1; i++){
if(arr[i] != arr[i+1]){
queue.add(arr[i]);
}
if(i+1 == arr.length-1){
queue.add(arr[i+1]);
}
}
int[] answer = new int[queue.size()];
int temp = 0;
while(!queue.isEmpty()){
answer[temp] = queue.poll();
temp++;
}
return answer;
}
}
통과
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
Queue<Integer> queue = new LinkedList<Integer>();
int temp = 10;
for(int i=0; i < arr.length; i++){
if(temp != arr[i]){
temp = arr[i];
queue.add(arr[i]);
}
}
int[] answer = new int[queue.size()];
temp = 0;
while(!queue.isEmpty()){
answer[temp] = queue.poll();
temp++;
}
return answer;
}
}
다른 사람의 코드를 보니 수가 0~9 사이라는 것을 이용해서 임시로 10을 넣고 인덱스 0에서부터 비교하고 다르면 임시변수와 큐에 넣어서 저장하는 그런 방법도 있었다.
효율성도 큰 차이는 없었지만 문제를 이용하는 그런건 좋은 것 같다
Queue.poll() 큐를 pull한다.
Queue.isEmpty() 큐가 비어있는지 확인한다.