Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.
For example, given[3, 30, 34, 5, 9]
, the largest formed number is9534330
.
Note: The result may be very large, so you need to return a string instead of an integer.
分析
理解是什么意思? Arrays.sort(array, (sA, sB)->(sB + sA).compareTo(sA + sB));
代码
class Solution {
public String largestNumber(int[] a) {
// int[] --> String[]
String[] array = new String[a.length];
for(int i = 0; i< a.length; i++){
array[i] = a[i] +"";
}
// sort string
Arrays.sort(array, (sA, sB)->(sB + sA).compareTo(sA + sB));
// string[] -> result
String result = String.join("", array);
return result.charAt(0) =='0' ? "0" :result;
}
}