java

[프로그래머스 - 자바] 소인수분해

tues 2023. 9. 1. 14:57

문제설명

 

 

솔루션

import java.util.*;

class Solution {
    public int[] solution(int n) {
        HashSet<Integer> set = new HashSet<>(); //중복되지 않기 때문
        
        for(int i = 2; i <= n; i++){
            while(n % i == 0){
                set.add(i);
                n /= i;
            }
        }
        if(n != 1){
            set.add(n);
        }
        return set.stream().mapToInt(Integer::intValue).sorted().toArray();
    }
}