Algorithm/프로그래머스

[Lv. 0] 소인수분해

녱녱 2023. 4. 10.

✏️ 작성한 코드

class Solution {
    public int[] solution(int n) {
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
        for (int i = 2; i <= Math.sqrt(n); i++) {
            while (n % i == 0) {
                set.add(i);
                n /= i;
            }
        }
        if (n != 1) set.add(n);

        return set.stream().mapToInt(Integer::intValue).toArray();
    }
}

애초에 중복을 방지하기 위해 set을 사용해보았다!

'Algorithm > 프로그래머스' 카테고리의 다른 글

[Lv. 0] 잘라서 배열로 저장하기  (0) 2023.04.13
[Lv. 0 ] 7의 개수  (0) 2023.04.11
[Lv. 0] 이진수 더하기  (0) 2023.04.07
[Lv. 0] 숨어있는 숫자의 덧셈 (2)  (0) 2023.04.06
[Lv. 0] 가까운 수  (0) 2023.04.04

댓글