-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode179.java
More file actions
47 lines (42 loc) · 1.5 KB
/
LeetCode179.java
File metadata and controls
47 lines (42 loc) · 1.5 KB
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
42
43
44
45
46
47
import java.util.Arrays;
import java.util.Comparator;
public class LeetCode179 {
public static void main(String[] args) {
// 输入:nums = [10,2]
// 输出:"210"
System.out.println(new Solution179().largestNumber(new int[] { 10, 2 }));
// 输入:nums = [3,30,34,5,9]
// 输出:"9534330"
System.out.println(new Solution179().largestNumber(new int[] { 3, 30, 34, 5, 9 }));
// 输入:nums = [999999991,9]
// 输出:"9999999991"
System.out.println(new Solution179().largestNumber(new int[] { 999999991, 9 }));
// 输入:nums = [0,0]
// 输出:"0"
System.out.println(new Solution179().largestNumber(new int[] { 0, 0 }));
}
}
class Solution179 {
public String largestNumber(int[] nums) {
String[] stringNums = new String[nums.length];
for (int i = 0; i < stringNums.length; i++) {
stringNums[i] = String.valueOf(nums[i]);
}
Arrays.sort(stringNums, new Comparator<>() {
public int compare(String a, String b) {
String first = b + a;
String second = a + b;
return first.compareTo(second);
}
});
// System.out.println(Arrays.toString(integerNums));
if (stringNums[0].equals("0")) {
return "0";
}
StringBuilder sb = new StringBuilder();
for (String item : stringNums) {
sb.append(item);
}
return sb.toString();
}
}