Published May 17, 2022
[
 
]
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.
Input: nums = [10,2]
Output: "210"
Input: nums = [3,30,34,5,9]
Output: "9534330"
1 <= nums.length <= 100
0 <= nums[i] <= 10^9
function largestNumber(nums: number[]): string {
let result: string;
nums.sort((a, b) => {
let ba: number = parseInt(b.toString() + a.toString());
let ab: number = parseInt(a.toString() + b.toString());
return ba - ab;
});
result = nums.join('');
if (result[0] === '0')
result = '0';
return result;
};