Largest Number leetcode原题 会要求你提供 测试数据
Page 1 of 1
Largest Number leetcode原题 会要求你提供 测试数据
一般题,自己写个comparetor搞定
public String largestNumber(int[] num) {
if(num == null || num.length == 0) return "";
String[] s_num = new String[num.length];
for(int i = 0; i < num.length; i++)
s_num[i] = String.valueOf(num[i]);
Comparator<String> comp = new Comparator<String>(){
@Override
public int compare(String str1, String str2){
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
}
};
Arrays.sort(s_num, comp);
if(s_num[0].charAt(0) == '0') return "0";
StringBuilder sb = new StringBuilder();
for(String s: s_num)
sb.append(s); return sb.toString();
}
public String largestNumber(int[] num) {
if(num == null || num.length == 0) return "";
String[] s_num = new String[num.length];
for(int i = 0; i < num.length; i++)
s_num[i] = String.valueOf(num[i]);
Comparator<String> comp = new Comparator<String>(){
@Override
public int compare(String str1, String str2){
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
}
};
Arrays.sort(s_num, comp);
if(s_num[0].charAt(0) == '0') return "0";
StringBuilder sb = new StringBuilder();
for(String s: s_num)
sb.append(s); return sb.toString();
}
Similar topics
» Number Of Digit One
» Meeting Room I leetcode原题
» Meeting Room II leetcode原题
» NumberOfIslandsI origianl questions:求岛屿数量 leetcode原题
» Meeting Room I leetcode原题
» Meeting Room II leetcode原题
» NumberOfIslandsI origianl questions:求岛屿数量 leetcode原题
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|