Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

프로그래머스 약수 구하기(자바) 본문

JavaAlgorithm

프로그래머스 약수 구하기(자바)

MyDiaryYo 2023. 12. 7. 14:26

 

 

 

class Solution {
    public int[] solution(int n) {
        int[] answer = {};
        ArrayList<Integer> answerL = new ArrayList<>();	//크기를 고정시킬 수 없기때문에 List생성

        for (int i=1; i<=n; i++) {	//List에 n의 약수를 구해 넣어준다
            if(n%i==0){
                answerL.add(i);
            }
        }

        answer = new int[answerL.size()];	//배열 answer크기를 정해진 List의 크기로 고정해준다
        for (int j=0; j<answerL.size(); j++){	//List 요소들을 배열 answer에 넣어준다
            answer[j] = answerL.get(j);
        }
        return answer;
    }
}