把数组排成最小的数

题目

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


我的想法

这道题比较容易想到的都是排序,排序后只要将数字拼接成一个字符串就可以了。下面是两个版本的代码,一个是自己实现比较和排序,一个是借助于Collections这个工具类的sort方法,需要重写compare方法。


代码如下

public String PrintMinNumber(int [] numbers) {
   if (numbers == null || numbers.length == 0) return "";
   MyComparator myComparator = new MyComparator();
    List<Integer> list = new ArrayList<Integer>();
    for (int i : numbers) {
        list.add(i);
    }
    Collections.sort(list, myComparator);
    StringBuilder sb = new StringBuilder();
    for (Integer val : list) {
        sb.append(val);
    }
    return sb.toString();
}

private class MyComparator implements Comparator < Integer > {

    @Override
    public int compare(Integer o1, Integer o2) {
        String s1 = String.valueOf(o1);
        String s2 = String.valueOf(o2);
        String str1 = s1+s2;
        String str2 = s2+s1;
        return str1.compareTo(str2);
    }
}

public String PrintMinNumber(int[] numbers) {
    ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
    StringBuilder str = new StringBuilder();

    for (int i : numbers) {
        if (list.isEmpty()) {
            ArrayList<Integer> innerList = new ArrayList<Integer>();
            innerList.add(i);
            list.add(innerList);
        }
        else {
            sortAdd(list, i);
        }

        // System.out.println("本轮循环添加" + i);
        // for(ArrayList<Integer> innerList : list) {
            // for (Integer it : innerList) {
                // System.out.print(it);
            // }
        // }
        // System.out.println();

    }

    for(ArrayList<Integer> innerList : list) {
        for (Integer it : innerList) {
            str.append(it.toString());
        }
    }

    return str.toString();

}

public void sortAdd(ArrayList< ArrayList< Integer >> list, int i) {
    for (ArrayList<Integer> innerList : list) {
        if (getFirst(innerList.get(0)) > getFirst(i)) {
            ArrayList<Integer> newInner = new ArrayList<Integer>();
            newInner.add(i);
            list.add(list.indexOf(innerList), newInner);
            return;
        }
        else if (getFirst(innerList.get(0)) == getFirst(i)) {
            String newInsert = i + "";
            for (Integer it : innerList) {
                String itString = it.toString();
                int length = newInsert.length() > itString.length() ? newInsert.length() : itString.length();
                for (int j = 0; j < length; j++) {
                    char newChar,itChar;
                    if (j < newInsert.length()) {
                        newChar = newInsert.charAt(j);
                    }
                    else{
                        newChar = newInsert.charAt(0);
                    }

                    if (j < itString.length()) {
                        itChar = itString.charAt(j);
                    }
                    else{
                        itChar = itString.charAt(0);
                    }
                    if (newChar < itChar) {
                        innerList.add(innerList.indexOf(it), i);
                        return;
                    }
                }
            }
            innerList.add(i);
            return;
        }
    }
    ArrayList<Integer> newInner = new ArrayList<Integer>();
    newInner.add(i);
    list.add(newInner);

}

public int getFirst(int n) {
    while (n / 10 != 0)
        n /= 10;
    return n;
}
posted @ 2018-05-12 13:17  _weirdly  阅读(280)  评论(0编辑  收藏  举报