class Solution { public String largestNumber(int[] nums) { String[] numStrs = new String[nums.length]; for(int i = 0; i < nums.length; ++ i) { numStrs[i] = String.valueOf(nums[i]); } Arrays.sort(numStrs, (o1,o2)->(o1+o2).compareTo(o2+o1)); StringBuilder sb = new StringBuilder(); for(int i = nums.length-1; i >= 0; -- i) { sb.append(numStrs[i]); } if(sb.charAt(0) == '0') return "0"; return sb.toString(); } }
import java.util.Arrays; public class TestArray { public static String largestNumber(int[] nums) { String[] numStrs = new String[nums.length]; for(int i = 0; i < nums.length; ++ i) { numStrs[i] = String.valueOf(nums[i]); } Arrays.sort(numStrs, (o1,o2)->(o1+o2).compareTo(o2+o1)); StringBuilder sb = new StringBuilder(); for(int i = nums.length-1; i >= 0; -- i) { sb.append(numStrs[i]); } if(sb.charAt(0) == '0') return "0"; return sb.toString(); } public static void main(String args[]){ int[] Nums = {1,2,3,4,5,6}; String aaa = largestNumber(Nums); System.out.println(aaa); } }