class Solution {
fun solution(numList: IntArray, n: Int): Array<IntArray> {
var count = 0
val answer = Array(numList.size / n) { IntArray(n) }
for (i in 0 until (numList.size / n)) {
for (j in 0 until n) {
answer[i][j] = numList[count]
count++
}
}
return answer
}
}
class Solution {
fun solution(num_list: IntArray, n: Int) = num_list.toList().chunked(n)
}
fun <T> Iterable<T>.chunked(size: Int): List<List<T>>
이 컬렉션을 각각 주어진 크기를 초과하지 않는 목록 목록으로 분할합니다
결과 목록의 마지막 목록에는 지정된 크기 보다 적은 수의 요소가 있을 수 있습니다
쉽게 말해서 개수를 기준으로 문자열을 자른다
ex)
val words = "one two three four five six seven eight nine ten".split(' ')
val chunks = words.chunked(3)
println(chunks) // [[one, two, three], [four, five, six], [seven, eight, nine], [ten]]
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/chunked.html
https://runebook.dev/ko/docs/kotlin/api/latest/jvm/stdlib/kotlin.collections/chunked