반응형
class Solution {
fun solution(x: Int, n: Int): LongArray {
// n 크기의 배열 선언
val answer = LongArray(n)
for (i in 0 until n) {
// 둘다 가능
answer[i] = (i + 1) * x.toLong()
// answer.set(i, ((i+1) * x.toLong())
}
return answer
}
}
코틀린으로 알고리즘 풀려니 헷갈려,,
class Solution {
fun solution(x: Int, n: Int): LongArray {
// n 크기의 배열 선언
val answer = LongArray(n){i->
x.toLong() * (i + 1).toLong()
}
return answer
}
}
→
[Kotlin 문법] 배열(Array)
# 배열 생성하는 방법. 코틀린에서 배열은 Array 클래스를 타입으로 가진다. 배열을 생성하는 방법은 2가지로 라이브러리 함수를 사용하는 방법, Array클래스의 생성자를 사용하는 방법이 있다. 1
dybz.tistory.com
class Solution {
fun solution(x: Int, n: Int): LongArray = LongArray(n) {i ->
x.toLong() * (i + 1).toLong()
}
}
→
class Solution {
fun solution(x: Int, n: Int): LongArray = LongArray(n) {
x.toLong() * (it + 1).toLong()
}
}
toLong - Kotlin Programming Language
kotlinlang.org
반응형
'IT > 알고리즘' 카테고리의 다른 글
68] 프로그래머스 세균 증식 Kotlin (0) | 2023.03.20 |
---|---|
67] kotlin 프로그래머스/ 행렬의 덧셈 (0) | 2021.09.29 |
65] Kotlin 프로그래머스 음양 더하기 (0) | 2021.09.28 |
64★★★★] 카카오 블라인드 / 순위 검색 (0) | 2021.09.07 |
62★★★] 카카오 블라인드 / 메뉴 리뉴얼 /C++ (0) | 2021.08.28 |
댓글