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:
- 1 <= nums.length <= 100
- 0 <= nums[i] <= 10^9
코드
class Solution {
fun largestNumber(nums: IntArray): String {
// if(nums.isEmpty()) return ""
val answer = nums
.map { it.toString() }
.sortedWith { a, b -> (b + a).compareTo(a + b) }
return if (answer[0][0] == '0') "0" else answer.joinToString("")
}
}
[Kotlin][Collection] sortedWith
지정된 비교기에 따라 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 서로 상대적인 순서를 유지함을 의미합니다.public fun Iterable.sortedWith(comparat
dev-baik.tistory.com
[Kotlin][Comparison Operation] compareTo
순서를 위해 이 개체를 지정된 개체와 비교합니다. 이 개체가 지정된 다른 개체와 같으면 0을 반환하고, 다른 개체보다 작으면 음수를, 다른 개체보다 크면 양수를 반환합니다.public interface Compara
dev-baik.tistory.com