Biggest Number From Gluing Numbers Together
Problem statement
You are given a list of non-negative integers nums. Arrange them in any order and write them one after another, with no separators, to make one long number. Return the largest number you can make, as a string.
- Every number is used exactly once, and its own digits stay in order.
- The answer has no leading zeros: if the largest arrangement is all zeros, return
"0".
Function
largestConcatenation(nums: int[]) → StringExamples
Example 1
nums = [8,81,89]return = "89881"Compare pairs by gluing them both ways. 89 before 8 gives 898, which beats 889. 8 before 81 gives 881, which beats 818. So the order is 89, 8, 81, and the result is "89881".
Example 2
nums = [121,12]return = "12121"12 then 121 gives 12121, while 121 then 12 gives 12112. The first is larger, even though 121 is the larger number.
Example 3
nums = [0,0,0]return = "0"Gluing gives 000, which is the number 0. It is written without extra zeros, as "0".
Constraints
1 <= nums.length <= 1000 <= nums[i] <= 10^9