LeetCode in Kotlin

179. Largest Number

Medium

Given 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: “9534330”

Constraints:

Solution

class Solution {
    fun largestNumber(nums: IntArray): String {
        val n = nums.size
        val s = arrayOfNulls<String>(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
    }
}