728x90
문제 설명
정수 리스트 num_list와 정수 n이 주어질 때, num_list의 첫 번째 원소부터 n 번째 원소까지의 모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요.
내가 작성한 답
class Solution {
public int[] solution(int[] num_list, int n) {
int[] answer = new int[n];
for(int i=0; i<n; i++){
answer[i] = num_list[i];
}
return answer;
}
}
다른 풀이
import java.util.Arrays;
class Solution {
public int[] solution(int[] num_list, int n) {
return Arrays.copyOfRange(num_list, 0, n);
}
}
728x90
'기타 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Lv.0 a와 b 출력하기 - 자바(java) (0) | 2023.09.10 |
---|---|
[프로그래머스] Lv.0 n의 배수 - 자바(java) (0) | 2023.09.01 |
[프로그래머스] Lv.0 카운트 업 - 자바(java) (0) | 2023.08.26 |
[프로그래머스] Lv.0 제곱수 판별하기 - 자바(java) (0) | 2023.08.25 |
[프로그래머스] Lv.0 공배수 - 자바(java) (0) | 2023.08.25 |