微软算法100题68 用数组排成最小的数

68.把数组排成最小的数。
题目:输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的
一个。
例如输入数组{32, 321},则输出这两个能排成的最小数字32132。
请给出解决问题的算法,并证明该算法

 1 package com.rui.microsoft;
 2 
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 
 6 public class Test68_Minimum {
 7 
 8     public static void main(String[] args) {
 9         int[] a = {3,2,123};
10         Test68_Minimum app = new Test68_Minimum();
11         String res = app.smallest(a);
12         System.out.println(res);
13     }
14     
15     String smallest(int[] a){
16         Integer[] aux = new Integer[a.length];
17         for(int i = 0; i < a.length; i++){
18             aux[i] = a[i];
19         }
20         
21         Arrays.sort(aux, new Comparator<Integer>() {
22 
23             @Override
24             public int compare(Integer o1, Integer o2) {
25                 return ("" + o1 + o2).compareTo("" + o2 + o1);
26             }
27         });
28         
29         StringBuilder sb = new StringBuilder();
30         for(int i = 0; i < aux.length; i++){
31             sb.append(aux[i]);
32         }
33         return sb.toString();
34     }
35 }
原文地址:https://www.cnblogs.com/aalex/p/5025629.html