严氏北美IT公司面试真题汇总和解答论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Largest Number leetcode原题 会要求你提供 测试数据

Go down

Largest Number leetcode原题 会要求你提供 测试数据 Empty Largest Number leetcode原题 会要求你提供 测试数据

Post by Admin Sun Oct 22, 2017 2:20 pm

一般题,自己写个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();
}

Admin
Admin

Posts : 124
Join date : 2017-10-21

https://csinterviewquestions.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum