Kotlin147 [Kotlin][Collection] zip 이 배열과 동일한 인덱스를 가진 다른 배열의 요소로 구성된 쌍 목록을 반환합니다. 반환된 목록에는 가장 짧은 컬렉션의 길이가 있습니다.public inline fun Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List { val first = iterator() val second = other.iterator() val list = ArrayList(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) while (first.hasNext() && second.hasNext()) { list.add(transform(first.n.. 2024. 8. 22. [Kotlin][Collection] minus 주어진 요소가 처음으로 나타나는 경우 없이 원본 컬렉션의 모든 요소를 포함하는 목록을 반환합니다.public operator fun Iterable.minus(elements: Iterable): List { val other = elements.convertToListIfNotCollection() if (other.isEmpty()) return this.toList() return this.filterNot { it in other }}val string = "HelloWorld"val intArray = intArrayOf(0, 1, 2, 3)print(string.indices - intArray.toSet()) // [4, 5, 6, 7, 8, 9] minus.. 2024. 8. 22. [LeetCode][Kotlin] 2073. Time Needed to Buy Tickets 2073. Time Needed to Buy TicketsThere are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line. You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i]. Each person takes exactly 1 second to buy a ticket. A person can only .. 2024. 8. 21. [Kotlin][Collection] plus 원래 컬렉션의 모든 요소와 지정된 요소 컬렉션의 모든 요소를 포함하는 목록을 반환합니다.public operator fun Collection.plus(elements: Iterable): List { if (elements is Collection) { val result = ArrayList(this.size + elements.size) result.addAll(this) result.addAll(elements) return result } else { val result = ArrayList(this) result.addAll(elements) return result }}val list.. 2024. 8. 21. [Kotlin][Collection] partition 원래 배열을 목록 쌍으로 분할합니다. 여기서 첫 번째 목록에는 조건자가 true 를 생성한 요소가 포함되고, 두 번째 목록에는 조건자가 false 를 생성한 요소가 포함됩니다.public inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> { val first = ArrayList() val second = ArrayList() for (element in this) { if (predicate(element)) { first.add(element) } else { second.add(element) } } return Pair.. 2024. 8. 21. [Kotlin][Collection] lastIndexOf 지정된 startIndex 부터 시작하여 지정된 문자가 마지막으로 나타나는 이 문자 시퀀스 내의 인덱스를 반환합니다.반환 char 의 마지막 발생 인덱스를 반환하거나, 아무것도 발견되지 않으면 -1 을 반환합니다.public fun CharSequence.lastIndexOf(string: String, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int { return if (ignoreCase || this !is String) indexOf(string, startIndex, 0, ignoreCase, last = true) else nativeLastIndexOf(string, startIndex)}v.. 2024. 8. 21. [Algorithm] 동적 계획법 (Dynamic Programming, DP) 동적 계획법 (Dynamic Programming, DP)복잡한 문제를 간단한 여러 개의 문제로 나누어 푸는 방법한 가지 문제에 대해서, 단 한 번만 풀도록 만들어주는 알고리즘즉, 똑같은 연산을 반복하지 않도록 만들어준다.실행 시간을 줄이기 위해 많이 이용되는 수학적 접근 방식의 알고리즘주어진 문제를 풀기 위해서, 문제를 여러 개의 하위 문제로 나누어 푼 다음, 그것을 결합하여 최종적인 목적에 도달하는 것하나의 큰 문제를 여러 개의 작은 문제로 나누고, 각 문제의 결과를 저장하여 다시 큰 문제를 해결하는 데 사용한다.일반적인 재귀를 단순히 사용 시 동일한 작은 문제들이 여러 번 반복되어 비효율적인 계산될 수 있다.예를 들어 피보나치 수를 구하고 싶을 때 재귀로 함수를 구성하면 O(2ⁿ) 으로, 동적 계획.. 2024. 8. 20. 이전 1 ··· 12 13 14 15 16 17 18 ··· 21 다음