整数数组能组成的最大数字

public class Temp {

    public static String largestNumber(int[] nums) {
        String[] numStrArray = new String[nums.length];

        for (int i = 0; i < nums.length; i++) {
            numStrArray[i] = String.valueOf(nums[i]);
        }

        Arrays.sort(numStrArray, (a, b) -> (b + a).compareTo(a + b));

        StringBuilder builder = new StringBuilder();
        for (String str : numStrArray) {
            builder.append(str);
        }

        return builder.charAt(0) == '0' ? "0" : builder.toString();
    }

    public static void main(String[] args) {
        int[] nums = {3, 30, 34, 5, 9};
        System.out.println(largestNumber(nums));//9534330
    }
}

你可能感兴趣的:(编程学习)