https://school.programmers.co.kr/learn/courses/30/lessons/181920
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
🥦 처음에 로직은 start_num 시작 인덱스 부터 end_num 인덱스까지 for문을 순회하면서 값을 담으려고 했는데 배열의 인덱스가 0부터시작하는게 아니라 생각한것보다 코드량이 점점 늘어나서 다른 방법을 참고 했다 end_num - start_num 사이의 숫자만 배열에 넣을꺼니 end_num - start_num의 크기만 안다면 for문을 i=0값부터 순회할 수 있었다
🍉 JAVA
class Solution {
public int[] solution(int start_num, int end_num) {
int[] answer = new int[end_num - start_num +1];
for (int i=0; i<answer.length; i++) {
answer[i] = start_num + i;
}
return answer;
}
}
🔥(start_num .. end_num).toList() 사이값을 담아주는 intRange()는 IntArray()에는 제공되지 않아 toList()로 사용하고 다시 .toIntArray()로 반환값 변환
⏰Kotlin
class Solution {
fun solution(start_num: Int, end_num: Int): IntArray = (start_num .. end_num).toList().toIntArray()
} //1
class Solution {
fun solution(start_num: Int, end_num: Int): List<Int> = (start_num .. end_num).toList()
} //2