์ค๋์ ํ์ต ํค์๋
๋ฌธ์ - ๋ฏธ๋ค๋ฌ
๊ฐ์ ์ซ์๋ ์ซ์ด
ํ์ด 1
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
ArrayList<Integer> list = new ArrayList<>();
list.add(arr[0]);
for(int i = 1; i < arr.length; i++) {
if(list.get(list.size() - 1) == arr[i]) continue;
else list.add(arr[i]);
}
int[] answer = new int[list.size()];
for(int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}
return answer;
}
}
ํ์ด 2
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
Stack<Integer> stack = new Stack<>();
for(int num : arr) {
if(stack.size() == 0 || stack.peek() != num) {
stack.push(num);
}
}
int[] answer = new int[stack.size()];
for(int i = stack.size() - 1; i >= 0; i--) {
answer[i] = stack.pop();
}
return answer;
}
}
- ์คํ์ผ๋ก๋ ํ์ดํ์ต๋๋ค.