본문 바로가기

Kotlin147

[LeetCode][Kotlin] 705. Design HashSet 705. Design HashSetDesign a HashSet without using any built-in hash table libraries. Implement MyHashSet class:void add(key) Inserts the value key into the HashSet.bool contains(key) Returns whether the value key exists in the HashSet or not.void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.내장된 해시 테이블 라이브러리를 사용하지 않고 HashSet을 디자인합니다.MyHashSet .. 2024. 8. 16.
[LeetCode][Kotlin] 448. Find All Numbers Disappeared in an Array 448. Find All Numbers Disappeared in an ArrayGiven an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.nums[i]가 [1, n] 범위에 있는 n개의 정수로 구성된 배열이 주어지면, nums에 나타나지 않는 [1, n] 범위의 모든 정수 배열을 반환합니다. Example 1:Input: nums = [4,3,2,7,8,2,3,1]Output: [5,6] Example 2:Input: nums = [1,1]Output: [2] Constraints:n =.. 2024. 8. 16.
[Kotlin][Validation Operations] require 값이 false 인 경우 lazyMessage 를 호출한 결과와 함께 IllegalArgumentException 을 발생시킵니다.public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit { contract { returns() implies value } if (!value) { val message = lazyMessage() throw IllegalArgumentException(message.toString()) }}fun getIndices(count: Int): List { require(count >= 0) { "Count must be non-negativ.. 2024. 8. 16.
[Kotlin][Collection] take / takeLast 처음 n 개 요소를 포함하는 목록을 반환합니다.public fun Iterable.take(n: Int): List { require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() if (this is Collection) { if (n >= size) return toList() if (n == 1) return listOf(first()) } var count = 0 val list = ArrayList(n) for (item in this) { list.add(item) if (++count ==.. 2024. 8. 16.
[Kotlin][Collection] slice 지정된 인덱스 범위의 인덱스에 있는 요소를 포함하는 목록을 반환합니다.public fun List.slice(indices: IntRange): List { if (indices.isEmpty()) return listOf() return this.subList(indices.start, indices.endInclusive + 1).toList()}val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.slice(1..3)) // [two, three, four]println(numbers.slice(0..4 step 2)) // [one, three, five]printl.. 2024. 8. 16.
[Kotlin][Collection] filter / filterNot / filterIndexed 주어진 조건과 일치하는 요소만 포함하는 목록을 반환합니다.public inline fun Iterable.filter(predicate: (T) -> Boolean): List { return filterTo(ArrayList(), predicate)}public inline fun > Iterable.filterTo(destination: C, predicate: (T) -> Boolean): C { for (element in this) if (predicate(element)) destination.add(element) return destination}val numbers: List = listOf(1, 2, 3, 4, 5, 6, 7)val evenNumbers = number.. 2024. 8. 16.
[LeetCode][Kotlin] 496. Next Greater Element I 496. Next Greater Element IThe next greater element of some element x in an array is the first greater element that is to the right of x in the same array.You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2. For each 0 next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1. Return an a.. 2024. 8. 14.