标签:
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
import java.util.ArrayList;
public class Solution {
public String PrintMinNumber(int [] numbers) {
String[] numStr = new String[numbers.length];
for (int i = 0; i < numbers.length; i++) {
numStr[i] = String.valueOf(numbers[i]);
}
selectSort(numStr);
StringBuilder strb = new StringBuilder();
for (int j = 0; j < numStr.length; j++) {
strb.append(numStr[j]);
}
return strb.toString();
}
public int compareStr(String str1, String str2) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
sb1.append(str1);
sb1.append(str2);
sb2.append(str2);
sb2.append(str1);
return sb1.toString().compareTo(sb2.toString());
}
public void selectSort(String[] data){
for (int i = 0; i < data.length; i++) {
int k = i;
for (int j = data.length - 1; j >i; j--) {
if (compareStr(data[j], data[k]) < 0) k = j;
}
String temp = data[i];
data[i] = data[k];
data[k] = temp;
}
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5641592.html