코딩테스트/lv0

[프로그래머스 코딩테스트] java Lv.0 소인수분해

chantleman 2024. 8. 8. 09:51

    import java.util.*;
    class Solution {
        public int[] solution(int n) {

            List<Integer> list = new ArrayList();

            int temp=n;
            for(int i=2;i<=n;i++)
            {
                boolean chk=false;
                while(temp%i==0)
                {
                    temp/=i;
                    if(!chk) list.add(i);
                    chk=true;                
                }            
            }
            int[] answer = new int[list.size()];
            for(int i=0;i<list.size();i++)
            {
                answer[i] = list.get(i);
            }
            return answer;
        }
    }