把数组排成最小的数
题目描述
代码
import java .util.Arrays;
/**
* 标题:把数组排成最小的数
* 题目描述
* 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
* 题目链接:
*
*/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);
}
}
【每日寄语】 完美的背后是艰辛与无数风雨的洗礼,宁可脱一层皮也要飞起来, 越努力越幸运 。