Time O(n log n · L) for numbers of up to L digits Space O(n · L)
Convert to strings and sort with (a, b) -> (b + a).compareTo(a + b). Join. If the first character is '0', return "0".
Approach 1
import java.util.Arrays;
class Solution {
public String largestNumber(int[] nums) {
String[] s = new String[nums.length];
for (int i = 0; i < nums.length; i++) s[i] = String.valueOf(nums[i]);
Arrays.sort(s, (a, b) -> (b + a).compareTo(a + b));
if (s[0].equals("0")) return "0";
return String.join("", s);
}
}
Verdict: The comparator is transitive, so sorting by it is valid.