forked from huailian123/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179_largest_Number.java
More file actions
29 lines (23 loc) · 914 Bytes
/
Copy path179_largest_Number.java
File metadata and controls
29 lines (23 loc) · 914 Bytes
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
//http://xiaoyaoworm.com/blog/2015/04/08/%E6%96%B0leetcode-sort-5-largest-number/
public class Solution {
public String largestNumber(int[] num) {
String[] numStr = new String[num.length];
for(int i = 0; i < num.length; i++){
numStr[i] = String.valueOf(num[i]);
}
Comparator<String> comparator = new Comparator<String>(){
public int compare(String x, String y){
String leftRight = x.concat(y);
String rightLeft = y.concat(x);
return rightLeft.compareTo(leftRight);
}
};
Arrays.sort(numStr, comparator);
StringBuffer sb = new StringBuffer();
for(String n: numStr){
sb.append(n);
}
java.math.BigInteger result = new java.math.BigInteger(sb.toString());
return result.toString();
}
}