首页 > 代码库 > java 数组内的最大组合数

java 数组内的最大组合数

给定一个任意长度的java数组,求数组内的数能组合出来的最大整数比如说{9,98,123,32} 最大就是 99832123 

import java.util.Arrays;
import java.util.Comparator;

class Test {
    public static void main(String args[]) {

        int[] arr = { 12, 45, 356, 767, 33, 23, 35 };
        System.out.println(getMaxSum(arr));
    }

    public static String getMaxSum(int[] nums) {
        if (nums == null) {
            return "";
        }
        String[] numsStr = new String[nums.length];
        int i = 0;
        for (int num : nums) {
            numsStr[i++] = num + "";
        }
        Arrays.sort(numsStr, new Comparator<String>() {
            public int compare(String str1, String str2) {
                String temp1 = str1 + str2;
                String temp2 = str2 + str1;
                return (-temp1.compareTo(temp2));
            }
        });
        StringBuilder sb = new StringBuilder();
        for (String s : numsStr)
            sb.append(s);
        return sb.toString();
    }
}

 

java 数组内的最大组合数