본문 바로가기

Kotlin147

[LeetCode][Kotlin] 1758. Minimum Changes To Make Alternating Binary String 1758. Minimum Changes To Make Alternating Binary StringYou are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa. The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not. Return the minimum number of operations needed to make s.. 2024. 8. 20.
[Kotlin][Collection] last / lastOrNull 마지막 요소를 반환합니다.주어진 조건자와 일치하는 마지막 요소를 반환합니다.NoSuchElementException - 배열이 비어 있는 경우public fun List.last(): T { if (isEmpty()) throw NoSuchElementException("List is empty.") return this[lastIndex]}public inline fun List.last(predicate: (T) -> Boolean): T { val iterator = this.listIterator(size) while (iterator.hasPrevious()) { val element = iterator.previous() if (p.. 2024. 8. 20.
[Kotlin][Collection] indexOf / indexOfFirst / indexOfLast 요소의 첫 번째 인덱스를 반환하거나 컬렉션에 요소가 없으면 -1 을 반환합니다.public fun Iterable.indexOf(element: T): Int { if (this is List) return this.indexOf(element) var index = 0 for (item in this) { checkIndexOverflow(index) if (element == item) return index index++ } return -1}val list = listOf(12, 21, 33, 42)print(list.indexOf(21)) // 1 주어진 술어와 일치하는 첫 번째 요소의 인덱스를 반환하거나, 컬.. 2024. 8. 20.
[Kotlin][Collection] first / firstOrNull 첫 번째 요소를 반환합니다. 주어진 조건자와 일치하는 첫 번째 요소를 반환합니다.NoSuchElementException - 컬렉션이 비어 있는 경우public fun Iterable.first(): T { when (this) { is List -> return this.first() else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty.") return iterator.next() } }}public inline fun I.. 2024. 8. 20.
[Kotlin][Collection] elementAt / elementAtOrElse / elementAtOrNull 지정된 인덱스에 있는 요소를 반환하거나 인덱스가 이 컬렉션의 범위를 벗어나면 IndexOutOfBoundsException을 발생시킵니다.public fun Iterable.elementAt(index: Int): T { if (this is List) return get(index) return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") }}val list = listOf(1, 2, 3)println(list.elementAt(0)) // 1println(list.elementAt(2)) // 3// list.element.. 2024. 8. 19.
[Kotlin][Collection] contains 컬렉션에서 요소가 발견되면 true 를 반환합니다.public operator fun Iterable.contains(element: T): Boolean { if (this is Collection) return contains(element) return indexOf(element) >= 0}val map: Map = mapOf("x" to 1)println("map.contains(\"x\") is ${map.contains("x")}") // trueprintln("\"x\" in map is ${"x" in map}") // trueprintln("map.contains(\"y\") is ${map.contains("y")}") // falseprintln("\"y\".. 2024. 8. 19.
[LeetCode][Kotlin] 706. Design HashMap 706. Design HashMapDesign a HashMap without using any built-in hash table libraries. Implement the MyHashMap class:MyHashMap() initializes the object with an empty map.void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.int get(int key) returns the value to which the specified key is mapped, or -1 if this.. 2024. 8. 16.