본문 바로가기

Kotlin147

[LeetCode][Kotlin] 169. Majority Element 169. Majority ElementGiven an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.크기 n의 배열 num이 주어지면 대부분의 요소를 반환합니다. 다수 요소는 ⌊n/2⌋회 이상 나타나는 요소입니다. 대부분의 요소가 배열에 항상 존재한다고 가정할 수 있습니다. Example 1:Input: nums = [3,2,3]Output: 3 Example 2:Input: nums = [2,2,1,1,1,2.. 2024. 8. 14.
[LeetCode][Kotlin] 605. Can Place Flowers 605. Can Place FlowersYou have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots. Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false ot.. 2024. 8. 14.
[Kotlin][Collection] drop / dropLast 처음 n 개 요소를 제외한 모든 요소를 ​​포함하는 목록을 반환합니다.public fun Iterable.drop(n: Int): List { require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() val list: ArrayList if (this is Collection) { val resultSize = size - n if (resultSize (resultSize) if (this is List) { if (this is RandomAccess) { for (index in .. 2024. 8. 14.
[Kotlin][Collection] count 주어진 조건자와 일치하는 요소의 수를 반환합니다.public fun Iterable.count(): Int { if (this is Collection) return size var count = 0 for (element in this) checkCountOverflow(++count) return count}print(listOf(1, 2, 3, 4).count { it > 1 }) // 3 count - Kotlin Programming Language kotlinlang.org 2024. 8. 14.
[LeetCode][Kotlin] 118. Pascal's Triangle 118. Pascal's TriangleGiven an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:정수 numRows가 주어지면 Pascal의 삼각형의 첫 번째 numRows를 반환합니다. Pascal의 삼각형에서 각 숫자는 표시된 대로 바로 위에 있는 두 숫자의 합입니다.  Example 1:Input: numRows = 5Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] Example 2:Input: numRows = 1Output.. 2024. 8. 13.
[LeetCode][Kotlin] 49. Group Anagrams 49. Group AnagramsGiven an array of strings strs, group the anagrams together. You can return the answer in any order.문자열 strs 배열이 주어지면, 애너그램을 그룹화합니다. 어떤 순서로든 답을 반환할 수 있습니다. Example 1:Input: strs = ["eat","tea","tan","ate","nat","bat"]Output: [["bat"],["nat","tan"],["ate","eat","tea"]]Explanation:There is no string in strs that can be rearranged to form "bat".The strings "nat" and "tan" are anag.. 2024. 8. 13.
[Kotlin][Collection] average 컬렉션에 있는 요소의 평균 값을 반환합니다.public fun Iterable.average(): Double { var sum: Double = 0.0 var count: Int = 0 for (element in this) { sum += element checkCountOverflow(++count) } return if (count == 0) Double.NaN else sum / count}print(listOf(1, 2, 3).average()) // 2.0 average - Kotlin Programming Language kotlinlang.org 2024. 8. 13.