정수 리스트 num_list와 정수 n이 주어질 때, num_list의 첫 번째 원소부터 n 번째 원소까지의 모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요.
<생각과정>
for문으로 n번째 원소까지니까 n.size로 지정하면...?
class Solution {
public int[] solution(int[] num_list, int n) {
int[] answer = new int[n];
for(int i=0; i<num_list.length; i++){
answer[i] = num_list[i];
}
return answer;
}
}
는 에러..
answer / num_list / n / solution 다 쓸라하니까 이러나 저번처럼 answer 지우고 다시
class Solution {
public int[] solution(int[] num_list, int n) {
int[] solution = new int[n];
for(int i=0; i<n; i++){
solution[i] = num_list[i];
}
return solution;
}
}
성공
확실히 변수 최대한 지우고하는게 생각하기 조금 더 편한거같다

'코딩테스트 > 프로그래머스_코딩 기초 트레이닝' 카테고리의 다른 글
[프로그래머스] 문자열 섞기 (1) | 2023.06.02 |
---|---|
[프로그래머스] 배열 만들기 1 (0) | 2023.06.01 |
[프로그래머스] 카운트 업 (1) | 2023.05.30 |
[프로그래머스] n의 배수 (0) | 2023.05.30 |
[프로그래머스] 원소들의 곱과 합 (0) | 2023.05.30 |