-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestNumber.java
37 lines (28 loc) · 1.07 KB
/
LargestNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
class Solution {
public String largestNumber(int[] num) {
if(num == null || num.length == 0)
return "";
// Convert int array to String array, so we can sort later on
String[] strNum = new String[num.length];
for(int i = 0; i < num.length; i++)
strNum[i] = String.valueOf(num[i]);
// Comparator to decide which string should come first in concatenation
Comparator<String> comp = new Comparator<String>(){
@Override
public int compare(String str1, String str2){
String s1 = str1 + str2; //return true if s1 > s2 else false
String s2 = str2 + str1;
return s2.compareTo(s1); // reverse order here,so we can append() later
}
};
Arrays.sort(strNum, comp);
// An extreme edge case by lc, say you have only a bunch of 0 in your int array
if(strNum[0].charAt(0) == '0')
return "0";
StringBuilder sb = new StringBuilder();
for(String s: strNum)
sb.append(s);
return sb.toString();
}
}