[프로그래머스/Java] Lv.0 약수 구하기

febCho·2024년 4월 30일
0

코딩테스트

목록 보기
184/253
post-thumbnail

문제

정수 n이 매개변수로 주어질 때, n의 약수를 오름차순으로 담은 배열을 return하도록 solution 함수를 완성해주세요.

- 제한사항

  • 1 ≤ n ≤ 10,000

풀이

import java.util.List;
import java.util.ArrayList;

class Solution {
    public int[] solution(int n) {
        
        List<Integer> nList = new ArrayList<>();
        for(int i=1; i<=n; i++){
            if(n % i == 0){
                nList.add(i);
            }
        }
        
        int[] answer = new int[nList.size()];
        for(int i=0; i<nList.size(); i++){
            answer[i] = nList.get(i);
        }
    
        return answer;
    }
}

결과

profile
Done is better than perfect.

0개의 댓글