문제
내 풀이
import java.util.*;
class Solution {
public int[] solution(int n) {
int[] answer = {};
HashSet<Integer> primeFactor = new HashSet<Integer>();
while(n>1){
for(int i = 2 ; i <= n ; i++){
if(n%i==0){
primeFactor.add(i);
n /=i;
break;
}
}
}
ArrayList<Integer> primeFactor2 = new ArrayList<Integer>(primeFactor);
Collections.sort(primeFactor2);
answer = new int[primeFactor2.size()];
for(int i = 0 ; i < primeFactor2.size(); i++){
answer[i]= primeFactor2.get(i);
}
return answer;
}
}
다른 사람 풀이
import java.util.*;
class Solution {
public ArrayList solution(int n) {
ArrayList<Integer> answer = new ArrayList<>();
for(int i = 2; i <= n; i++){
while(n%i == 0){
if(!answer.contains(i))
answer.add(i);
n /= i;
}
}
return answer;
}
}