
내가 작성한 코드
import java.util.*;
public class Solution {
public int[] solution(int[] arr) {
Stack<Integer> stack = new Stack<>();
stack.push(arr[0]);
for (int i = 1; i < arr.length; i++) {
if (stack.peek() == arr[i]) {
continue;
} else {
stack.push(arr[i]);
}
}
int answer[] = new int[stack.size()];
for (int i = stack.size() - 1; i >= 0; i--) {
answer[i] = stack.pop();
}
return answer;
}
}
다른 사람의 코드
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
ArrayList<Integer> tempList = new ArrayList<Integer>();
int preNum = 10;
for(int num : arr) {
if(preNum != num)
tempList.add(num);
preNum = num;
}
int[] answer = new int[tempList.size()];
for(int i=0; i<answer.length; i++) {
answer[i] = tempList.get(i).intValue();
}
return answer;
}
}
Share article