由于最大数可能超过Integer.MAX_VALUE,返回值用String返回:
//正整数数组组成最大数
static String maxNum(int [] array) {
for (int i = 0;i < array.length;i ++) {
for (int j = i;j < array.length;j ++) {
int tmp;
int maxI =array[i];
int maxJ =array[j];
maxI = Integer.valueOf(String.valueOf(array[i]) + String.valueOf(array[j]));
maxJ = Integer.valueOf(String.valueOf(array[j]) + String.valueOf(array[i]));
if (maxI > maxJ) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = array.length - 1;i >= 0;i --) {
stringBuilder.append(array[i]);
}
return stringBuilder.toString();
}
for循环内其实是一个冒泡法的变体,排好序再拼接就行了,时间复杂度为O(n2)