본문 바로가기

Kotlin147

[Kotlin][Char] getNumericValue 지정된 유니코드 문자가 나타내는 int 값을 반환합니다. 예를 들어, 문자 '\u216C' (로마 숫자 50) 는 값 50 을 갖는 int 를 반환합니다.대문자( '\u0041' ~ '\u005A' ), 소문자( '\u0061' ~ '\u007A' ), 전자 변형( '\uFF21' ~ '\uFF3A' 및 '\uFF41' ~ '\uFF5A' ) 형태의 문자 AZ 는 10~35 의 숫자 값을 갖습니다. 이는 이러한 char 값에 숫자 값을 할당하지 않는 Unicode 사양과는 별개입니다.문자에 숫자 값이 없으면 -1 이 반환됩니다. 문자에 음이 아닌 정수로 표현할 수 없는 숫자 값(예: 분수 값) 이 있으면 -2 가 반환됩니다.public static int getNumericValue(char ch) { .. 2024. 9. 3.
[Kotlin][String] toInt 문자열을 Int 숫자로 구문 분석하고 결과를 반환합니다.NumberFormatException - 문자열이 숫자의 유효한 표현이 아닌 경우.public actual inline fun String.toInt(): Int = java.lang.Integer.parseInt(this)print("2".toInt()) // 2val char = '1'println(char.toString().toInt()) // 1 public actual inline fun String.toInt(radix: Int): Int = java.lang.Integer.parseInt(this, checkRadix(radix))print("101".toInt(2)) // 5 toInt - Kotlin Programming Lang.. 2024. 9. 3.
[Kotlin][Transformation Operation] ceil / floor / round import kotlin.math.* 주어진 값 x 를 양의 무한대 방향의 정수로 반올림합니다.public actual inline fun ceil(x: Double): Double = nativeMath.ceil(x)println(ceil(16.746)) // 17.0 주어진 값 x 를 음의 무한대 방향의 정수로 반올림합니다.public actual inline fun floor(x: Double): Double = nativeMath.floor(x)println(floor(16.746)) // 17.0 지정된 값 x를 짝수 방향으로 반올림하여 가장 가까운 정수 방향으로 반올림합니다.public actual fun round(x: Double): Double { if (x % 0.5 != 0.0).. 2024. 9. 2.
[Kotlin][Transformation Operation] sqrt / pow / hypot import kotlin.math.*값 x 의 양의 제곱근을 계산합니다.public actual inline fun sqrt(x: Double): Double = nativeMath.sqrt(x)println(sqrt(16.0)) // 4.0 이 값을 x 승으로 늘립니다.public actual inline fun Double.pow(n: Int): Double = nativeMath.pow(this, n.toDouble())println((16.0).pow(2}) // 256.0 중간 오버플로나 언더플로 없이 sqrt(x^2 + y^2)를 계산합니다.public actual inline fun hypot(x: Double, y: Double): Double = nativeMath.hypot(x, y)p.. 2024. 9. 2.
[Kotlin][Number] coerceAtMost maximumValue 보다 작거나 같으면 이 값을 반환하고, 그렇지 않으면 maximumValue 를 반환합니다.public fun > T.coerceAtMost(maximumValue: T): T { return if (this > maximumValue) maximumValue else this}println(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.SATURDAY)) // FRIDAYprintln(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.WEDNESDAY)) // WEDNESDAYprintln(10.coerceAtMost(5)) // 5println(10.coerceAtMost(20)) // 10 coerceAtMost - Kotl.. 2024. 9. 2.
[LeetCode][Kotlin] 179. Largest Number 179. Largest NumberGiven a list of non-negative integers nums, arrange them such that they form the largest number and return it.Since the result may be very large, so you need to return a string instead of an integer.음수가 아닌 정수 숫자 목록이 주어지면 가장 큰 숫자를 형성하도록 배열하여 반환합니다. 결과가 매우 클 수 있으므로 정수 대신 문자열을 반환해야 합니다. Example 1:Input: nums = [10,2]Output: "210" Example 2:Input: nums = [3,30,34,5,9]Output: "9534.. 2024. 8. 30.
[Kotlin][Collection] ifEmpty 비어 있지 않으면 이 배열을 반환하고, 배열이 비어 있으면 defaultValue 함수를 호출한 결과를 반환합니다.public inline fun C.ifEmpty(defaultValue: () -> R): R where C : Array, C : R { contract { callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE) } return if (isEmpty()) defaultValue() else this}val emptyArray: Array = emptyArray()val emptyOrNull: Array? = emptyArray.ifEmpty { null }println(emptyOrNull) // nullval e.. 2024. 8. 30.