-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestNumber.java
More file actions
49 lines (46 loc) · 1.38 KB
/
LargestNumber.java
File metadata and controls
49 lines (46 loc) · 1.38 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
48
49
package medium;
import java.util.*;
/**
* ClassName: LargestNumber.java
* Author: chenyiAlone
* Create Time: 2019/11/12 14:45
* Description: No.179 Largest Number
* Given a list of non negative integers, arrange them such that they form the largest number.
*
* Example 1:
*
* Input: [10,2]
* Output: "210"
* Example 2:
*
* Input: [3,30,34,5,9]
* Output: "9534330"
* Note: The result may be very large, so you need to return a string instead of an integer.
*
*/
public class LargestNumber {
public String largestNumber(int[] nums) {
String[] words = new String[nums.length];
boolean zero = true;
for (int i = 0; i < nums.length; i++) {
words[i] = Integer.toString(nums[i]);
if (nums[i] != 0) zero = false;
}
if (zero) return "0";
Arrays.sort(words, (a, b)-> {
StringBuilder sba = new StringBuilder(a), sbb = new StringBuilder(b);
sba.append(b);
sbb.append(a);
for (int i = 0; i < sba.length(); i++) {
if (sba.charAt(i) < sbb.charAt(i)) return 1;
else if (sba.charAt(i) > sbb.charAt(i)) return -1;
}
return 0;
});
StringBuilder ret = new StringBuilder();
for (String s : words) {
ret.append(s);
}
return ret.toString();
}
}