把数组排成最小的数

题目形容

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

题目链接: 把数组排成最小的数

代码

import java.util.Arrays;/** * 题目:把数组排成最小的数 * 题目形容 * 输出一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输出数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。 * 题目链接: * https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=13&&tqId=11185&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking */public class Jz32 {    public String printMinNumber(int[] numbers) {        if (numbers == null || numbers.length == 0) {            return "";        }        int n = numbers.length;        String[] nums = new String[n];        for (int i = 0; i < n; i++) {            nums[i] = numbers[i] + "";        }        Arrays.sort(nums, (s1, s2) -> (s1 + s2).compareTo(s2 + s1));        String result = "";        for (String str : nums) {            result += str;        }        return result;    }    public static void main(String[] args) {        Jz32 jz32 = new Jz32();        int[] numbers = new int[]{3, 32, 321};        String s = jz32.printMinNumber(numbers);        System.out.println(s);    }}
【每日寄语】 完满的背地是艰苦与有数风雨的洗礼,宁肯脱一层皮也要飞起来,越致力越侥幸。