Given a list of non negative integers, arrange them such that they form the largest number.
Example
Given [1, 20, 23, 4, 8]
,
the largest formed number is 8423201
.
public class Solution {
/**
*@param num: A list of non negative integers
*@return: A string
*/
public String largestNumber(int[] num) {
if(num.length == 0) return "";
String[] strList = new String[num.length];
for(int i = 0; i < num.length; i++) strList[i] = "" + num[i];
Arrays.sort(strList, new comparatorI());
int NonZero = 0;
while(NonZero < strList.length && "0".equals(strList[NonZero]))
NonZero++;
StringBuilder sb = new StringBuilder();
for(int i = NonZero; i < strList.length; i++){
sb.append(strList[i]);
}
return sb.length() == 0 ? "0" : sb.toString();
}
class comparatorI implements Comparator<String> {
public int compare(String s1, String s2) {
return (s2 + s1).compareTo(s1 + s2);
}
}
}
r。