-
Notifications
You must be signed in to change notification settings - Fork 0
/
179.最大数.java
67 lines (60 loc) · 1.52 KB
/
179.最大数.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
/*
* @lc app=leetcode.cn id=179 lang=java
*
* [179] 最大数
*
* https://leetcode-cn.com/problems/largest-number/description/
*
* algorithms
* Medium (34.49%)
* Likes: 184
* Dislikes: 0
* Total Accepted: 16.8K
* Total Submissions: 48.6K
* Testcase Example: '[10,2]'
*
* 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
*
* 示例 1:
*
* 输入: [10,2]
* 输出: 210
*
* 示例 2:
*
* 输入: [3,30,34,5,9]
* 输出: 9534330
*
* 说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
*
*/
// @lc code=start
class Solution {
public String largestNumber(int[] nums) {
if(nums.length == 0) {
return "0";
}
Integer[] arrs = new Integer[nums.length];
for(int i = 0; i < nums.length; i++) {
arrs[i] = nums[i];
}
Arrays.sort(arrs, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
String tempA = o1 + "" + o2;
String tempB = o2 + "" + o1;
return tempB.compareTo(tempA);
}
});
StringBuilder sb = new StringBuilder();
for(int i = 0; i < arrs.length; i++) {
sb.append(arrs[i]);
}
String result = sb.toString();
return result.charAt(0) == '0' ? "0" : result;
}
}
// @lc code=end