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
// #2022_10_13_Time_380_ms_(43.40%)_Space_39.9_MB_(60.38%)
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: String?, b: String? ->
(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
}
}