zoukankan      html  css  js  c++  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();
        }
    }
  • 相关阅读:
    第一次项目总结
    8.16 CSS知识点7
    2016y9m22d 博文分享
    2016y9m8d
    2016y9m7d
    2016y9m6d
    2016y9m5d
    2016.9.2博文分享!
    2016y8m16d
    2016y8m15d
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/5971355.html
Copyright © 2011-2022 走看看