본문 바로가기

Kotlin147

[Kotlin][Map] getOrDefault 지정된 키가 매핑된 값을 반환하거나, 이 맵에 키에 대한 매핑이 포함되어 있지 않은 경우 defaultValue를 반환합니다.public inline fun Map.getOrDefault(key: K, defaultValue: V): V = (this as Map).getOrDefault(key, defaultValue) val list = listOf(1, 2, 3, 4)val map = mutableMapOf()list.forEach { map[it] = map.getOrDefault(it, 0) + 1 }print(map) // {1=1, 2=1, 3=1, 4=1} getOrDefault - Kotlin Programming Language kotlinlang.org 2024. 9. 7.
[Kotlin][Array] copyOfRange 원래 배열의 지정된 범위의 복사본인 새 배열을 반환합니다.public actual inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { return if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0)) { copyOfRangeImpl(fromIndex, toIndex) } else { if (toIndex > size) throw IndexOutOfBoundsException("toIndex: $toIndex, size: $size") java.util.Arrays.copyOfRange(this, fromIndex, toIndex) }}val ar.. 2024. 9. 6.
[Kotlin][Array] sliceArray 지정된 인덱스 범위의 인덱스에 있는 요소를 포함하는 배열을 반환합니다.public fun Array.sliceArray(indices: IntRange): Array { if (indices.isEmpty()) return copyOfRange(0, 0) return copyOfRange(indices.start, indices.endInclusive + 1)}public actual inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { return if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0)) { copyOfRangeImpl(fromIndex, toIndex) .. 2024. 9. 6.
[Kotlin][Collection] removeFirst / removeLast / removeAt 이 변경 가능한 목록에서 첫 번째 요소를 제거하고 제거된 요소를 반환하거나, 이 목록이 비어 있으면 NoSuchElementException을 발생시킵니다.public fun MutableList.removeFirst(): T = if (isEmpty()) throw NoSuchElementException("List is empty.") else removeAt(0)val list = mutableListOf(1, 2, 3, 4)print(list.removeFirst()) // 1print(list) // [2, 3, 4] 이 변경 가능한 목록에서 마지막 요소를 제거하고 제거된 요소를 반환하거나, 이 목록이 비어 있으면 NoSuchElementException을 발생시킵니다.public fun M.. 2024. 9. 6.
[LeetCode][Kotlin] 1291. Sequential Digits 1291. Sequential DigitsAn integer has sequential digits if and only if each digit in the number is one more than the previous digit. Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.정수는 숫자의 각 숫자가 이전 숫자보다 하나 더 큰 경우에만 연속 숫자를 갖습니다. 연속 숫자가 있는 [낮음, 높음] 범위(포함)의 모든 정수의 정렬된 목록을 반환합니다. Example 1:Input: low = 100, high = 300Output: [123,234] Example 2.. 2024. 9. 5.
[LeetCode][Kotlin] 2870. Minimum Number of Operations to Make Array Empty 2870. Minimum Number of Operations to Make Array EmptyYou are given a 0-indexed array nums consisting of positive integers.There are two types of operations that you can apply on the array any number of times:Choose two elements with equal values and delete them from the array.Choose three elements with equal values and delete them from the array.Return the minimum number of operations required .. 2024. 9. 5.
[Kotlin][Collection] sortedWith 지정된 비교기에 따라 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 서로 상대적인 순서를 유지함을 의미합니다.public fun Iterable.sortedWith(comparator: Comparator): List { if (this is Collection) { if (size () as Array).apply { sortWith(comparator) }.asList() } return toMutableList().apply { sortWith(comparator) }}public actual fun MutableList.sortWith(comparator: Comparator): Unit { if (size > 1).. 2024. 9. 5.