g0101_0200.s0179_largest_number.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0101_0200.s0179_largest_number
// #Medium #Top_Interview_Questions #String #Sorting #Greedy
// #2023_03_02_Time_187_ms_(100.00%)_Space_36.7_MB_(76.56%)
class Solution {
fun largestNumber(nums: IntArray): String {
val n = nums.size
val s = arrayOfNulls(n)
for (i in 0 until n) {
s[i] = nums[i].toString()
}
s.sortWith { a, b -> (b + a).compareTo(a + b) }
val sb = StringBuilder()
for (str in s) {
sb.append(str)
}
val result = sb.toString()
return if (result.startsWith("0")) "0" else result
}
}