image.png
(图片来源https://leetcode-cn.com/problems/largest-number/
)
日期 | 是否一次通过 | comment |
---|---|---|
2020-03-19 | 0 |
public String largestNumber(int[] num) {
if(num == null || num.length == 0) {
return "";
}
// Convert int array to String array, so we can sort later on
List<String> sNum = Arrays.stream(num).
mapToObj(String::valueOf).
sorted((x,y) -> (y+x).compareTo(x+y)).
collect(Collectors.toList());
if(sNum.get(0).charAt(0) == '0') {
return "0";
}
return String.join("", sNum);
}
public String largestNumber2(int[] num) {
if(num == null || num.length == 0)
return "";
// Convert int array to String array, so we can sort later on
String[] sNum = new String[num.length];
for(int i = 0; i < num.length; i++){
sNum[i] = String.valueOf(num[i]);
}
Arrays.sort(sNum, (x,y) -> (y+x).compareTo(x+y));
// An extreme edge case by lc, say you have only a bunch of 0 in your int array
if(sNum[0].charAt(0) == '0'){
return "0";
}
return String.join("", sNum); // String.join("", s_num) 太耗时间了
}
网友评论