原理: 如果xy大于yx, yz 大于 zy ,则xz一定大于zx,即具有传递性
<?php
class Solution {
/**
* @param Integer[] $nums
* @return String
*/
function largestNumber($nums) {
usort($nums, function($x, $y){
$baseX = $baseY = 10;
while ($baseX <= $x) {
$baseX *= 10;
}
while ($baseY <= $y) {
$baseY *= 10;
}
return $y * $baseX + $x - $x * $baseY - $y;
});
return $nums[0] == 0 ? 0 : implode('', $nums);
}
}