将输入的数组排成最小的数

题目描述:
  输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。 
输入:

输入可能包含多个测试样例。
对于每个测试案例,输入的第一行为一个整数m (1<=m <=100)代表输入的正整数的个数。
输入的第二行包括m个正整数,其中每个正整数不超过10000000。 

输出:

对应每个测试案例,
输出m个数字能排成的最小数字。 

样例输入:

23 13 6
23456 56

样例输出:

13236
2345656
public class MiniNumber {

    public static long getMiniNumber(int[] positiveInts)
    {
        if(positiveInts.length == 0){
            return 0;
        }
        if(positiveInts.length == 1){
            return positiveInts[0];
        }
        
        for(int i=positiveInts.length-1;i>0;i--){
            for(int j=i-1;j>=0;j--){
                if(isBigger(positiveInts[j],positiveInts[i])){
                    int temp =positiveInts[i];
                    positiveInts[i] =positiveInts[j];
                    positiveInts[j] = temp;
                }
            }
        }
        try{
            long result = positiveInts[0];
            for(int i=0;i<positiveInts.length-1;i++){
        //拼接结果集成long
                result = (long)(result*Math.pow(10,(positiveInts[i+1]+"").length()))+positiveInts[i+1];
            }
            return result;
        }catch(Exception e){
            return -1;
        }
    }

    private static boolean isBigger(int x, int y) {
        //比较两个数字大小字典大小
        StringBuffer a = new StringBuffer().append(x);
        StringBuffer b = new StringBuffer().append(y);
        while(a.length() != b.length()){
            if(a.length()>b.length() && a.length()<b.length()*2){
                b.append(b.substring(0,a.length()-b.length()));
            }else if(a.length()<b.length() && a.length()*2>b.length()){
                a.append(a.substring(0,b.length()-a.length()));
            }else if(a.length()>=b.length()*2){
                b.append(b);
            }else{
                a.append(a);
            }
        }
        boolean result = false;
        for(int i=0;i<a.length();i++){
            if(a.charAt(i) < b.charAt(i)){
                result = false;
                break;
            }else if(a.charAt(i) > b.charAt(i)){
                result = true;
                break;
            }else{
                continue;
            }
        }
        return result;
    }
}
原文地址:https://www.cnblogs.com/jing99/p/7427939.html