정수 start와 end가 주어질 때, start부터 end까지의 숫자를 차례로 담은 리스트를 return하도록 solution 함수를 완성해주세요.
<생각과정>
int i <= start로 정의, i<= end 해주고 i++로 받아오는 반복문이면 되지않을까?
class Solution {
public int[] solution(int start, int end) {
for(int i=start; i<=end; i++){
solutoin.add[i];
}
return solution;
}
}
는 에러
일단 solution 철자 틀린거 고치고,,,
.add()로 바꿔주고
배열과 리스트를 혼동해서 써줘버렸다
class Solution {
public int[] solution(int start, int end) {
List<Integer> answer = new ArrayList<>();
for(int i=start; i<=end; i++){
answer.add(i);
}
int[] arr= new int[answer.size()];
for(int i =0; i<answer.size(); i++){
arr[i]= answer.get[i];
}
return arr;
}
}
또..
class Solution {
public int[] solution(int start, int end) {
List<Integer> answer = new ArrayList<>();
for(int i = start; i <= end; i++) {
answer.add(i);
}
int[] arr = new int[answer.size()];
for(int i = 0; i < answer.size(); i++) {
arr[i] = answer.get(i);
}
return arr;
}
}
같은 오류가 계속 반복하다 다시 읽어본 문제
"리스트를 return하도록 "
다시 시작
<생각다시>
일단 배열 선언 생성부터
int answer[] = new int[end-start+1];
배열길이는 end-start +1
처음 썼던 반복문으로
class Solution {
public int[] solution(int start, int end) {
int[] answer = new int[end - start + 1];
int a = 0;
for(int i = start; i <= end; i++){
answer[a++] = i;
}
return answer;
}
}
어렵다 어려워...
'코딩테스트 > 프로그래머스_코딩 기초 트레이닝' 카테고리의 다른 글
[프로그래머스] 배열 만들기 1 (0) | 2023.06.01 |
---|---|
[프로그래머스] n 번째 원소까지 (0) | 2023.05.31 |
[프로그래머스] n의 배수 (0) | 2023.05.30 |
[프로그래머스] 원소들의 곱과 합 (0) | 2023.05.30 |
[프로그래머스] 조건에 맞게 수열 변환하기 3 (2) | 2023.05.30 |