dev._.note

[프로그래머스] JAVA 0단계 뒤에서 5등까지 본문

Dev/코딩테스트

[프로그래머스] JAVA 0단계 뒤에서 5등까지

Laena 2023. 5. 24. 09:13

뒤에서 5등까지

정수로 이루어진 리스트 num_list가 주어집니다. num_list에서 가장 작은 5개의 수를 오름차순으로 담은 리스트를 return하도록 solution 함수를 완성해주세요.

 

풀이

import java.util.*;
class Solution {
    public int[] solution(int[] num_list) {
        int[] answer = new int[5];
        Arrays.sort(num_list);
        
        for(int i = 0; i < 5; i++){
            answer[i] = num_list[i];
        }
        return answer;
    }
}

 

결과

테스트 1
입력값 [12, 4, 15, 46, 38, 1, 14]
기댓값 [1, 4, 12, 14, 15]
실행 결과 테스트를 통과하였습니다.