
* 문제 풀이
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;
}
}
^n번째 원소^ 까지 이기 때문에 배열 길이를 n으로 설정.
for문을 통해 num_list의 앞에서부터 n의 배열 요소를 확인해 answer에 배열 요소 값을 반환.
* 프로그래머스 다른 풀이
import java.util.*;
class Solution {
public int[] solution(int[] num_list, int n) {
int[] answer = {};
answer = Arrays.copyOfRange(num_list,0,n);
return answer;
}
}
class Solution {
public int[] solution(int[] num_list, int n) {
int[] answer = new int[n];
for(int i=0; i<answer.length; i++){
answer[i] = num_list[i];
}
return answer;
}
}
'코테 > 프로그래머스 JAVA Lv.0' 카테고리의 다른 글
| [프로그래머스/java/Lv.0] 카운트 다운 (0) | 2025.02.20 |
|---|---|
| [프로그래머스/java/Lv.0] 첫 번째로 나오는 음수 (0) | 2025.02.19 |
| [프로그래머스/java/Lv.0] n 번째 원소부터 (0) | 2025.02.18 |
| [프로그래머스/java/Lv.0] n개 간격의 원소들 (0) | 2025.02.17 |
| [프로그래머스/java/Lv.0] 홀수 vs 짝수 (0) | 2025.02.15 |