-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution179.java
41 lines (32 loc) · 982 Bytes
/
Solution179.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
38
39
40
41
package algorithm.leetcode;
import java.util.Arrays;
/**
* @author: mayuan
* @desc: 最大数
* @date: 2019/01/14
*/
public class Solution179 {
public static void main(String[] args) {
int[] nums = {3, 30, 34, 5, 9};
System.out.println(new Solution179().largestNumber(nums));
}
public String largestNumber(int[] nums) {
if (null == nums || 0 >= nums.length) {
return "0";
}
String[] strNums = new String[nums.length];
for (int i = 0; i < nums.length; ++i) {
strNums[i] = String.valueOf(nums[i]);
}
Arrays.sort(strNums, (a, b) -> (b + a).compareTo(a + b));
// 特殊情况处理: [0, 0, 0] -> 0
if ('0' == strNums[0].charAt(0)) {
return "0";
}
StringBuilder stringBuilder = new StringBuilder();
for (String e : strNums) {
stringBuilder.append(e);
}
return stringBuilder.toString();
}
}