描述
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
示例1
输入:
[3,32,321]
复制返回值:
"321323"
public class Jz32_PrintMinNumber {
@Test
public void test(){
int[] arr = {3,32,321};
String s = PrintMinNumber(arr);
System.out.println(s);
}
public String PrintMinNumber(int[] numbers) {
String[] arr = new String[numbers.length];
for (int i = 0; i < numbers.length; i++) {
arr[i] = numbers[i] + "";
}
Arrays.sort(arr,(a,b)->(a+b).compareTo(b+a));
StringBuffer stringBuffer = new StringBuffer();
for (String s : arr) {
stringBuffer.append(s);
}
return stringBuffer.toString();
}
}