179.最大数
思路:
方法:自定义排序
class Solution {public String largestNumber(int[] nums) {if(nums == null || nums.length == 0){return "";}//将每个数字转换成字符串String[] strs = new String[nums.length];for(int i = 0;i < nums.length;i++){strs[i] = String.valueOf(nums[i]);}//自定义比较规则Arrays.sort(strs,new Comparator<String>() {public int compare(String x,String y) {//y+x > x+y --x,y需要交换,则x<y//y+x < x+y --x,y不需要交换,则x>yreturn (y+x).compareTo(x+y);}});//特殊情况if(strs[0].equals("0")){return "0";}StringBuilder sb = new StringBuilder();for(String str : strs){sb.append(str);}return sb.toString();}
}