题目描述
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solution {
public String PrintMinNumber(int [] numbers) {
StringBuffer sb = new StringBuffer();
if(numbers.length == 0) {
return "";
}
ArrayList<Integer> list = new ArrayList<>();
for(int x : numbers) {
list.add(x);
}
Collections.sort(list, new Comparator<Integer>(){
@Override
public int compare(Integer a, Integer b) {
String s1 = "" + a + b;
String s2 = "" + b + a;
return s1.compareTo(s2);
}
});
for(int x : list) {
sb.append(x + "");
}
return sb.toString();
}
}