题解 | #把数组排成最小的数#
把数组排成最小的数
https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param numbers int整型一维数组
* @return string字符串
*/
public String PrintMinNumber (int[] numbers) {
// write code here
//重载排序
if(numbers == null || numbers.length == 0) return "";
String[] nums = new String[numbers.length];
for(int i = 0; i < numbers.length; i ++){
nums[i] = numbers[i] + "";
}
Arrays.sort(nums, new Comparator<String>(){
public int compare(String s1, String s2){
return (s1 + s2).compareTo((s2 + s1));
}
});
String res = new String();
for(int i = 0; i < nums.length; i ++){
res += nums[i];
}
return res;
}
}
查看12道真题和解析