forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestNumber.java
More file actions
27 lines (25 loc) · 822 Bytes
/
LargestNumber.java
File metadata and controls
27 lines (25 loc) · 822 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
import java.util.Arrays;
import java.util.Comparator;
public class LargestNumber {
public String largestNumber(int[] nums) {
String[] strs = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
strs[i] = String.valueOf(nums[i]);
}
Arrays.sort(strs, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String s1 = o1 + o2;
String s2 = o2 + o1;
return s2.compareTo(s1);
}
});
StringBuilder sb = new StringBuilder();
for (String str : strs) {
sb.append(str);
}
int i = 0;
for ( ; i < sb.length() && sb.charAt(i) == '0'; i++);
return i >= sb.length() ? "0" : sb.substring(i);
}
}