-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLeetCode179.java
More file actions
42 lines (38 loc) · 1.13 KB
/
LeetCode179.java
File metadata and controls
42 lines (38 loc) · 1.13 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
package problems;
import java.util.Arrays;
import java.util.PriorityQueue;
public class LeetCode179 {
public String largestNumber(int[] nums) {
int n = nums.length;
Integer[] numsArr = new Integer[n];
for (int i = 0; i < n; i++) {
numsArr[i] = nums[i];
}
Arrays.sort(numsArr, (x, y) -> {
long sx = 10, sy = 10;
while (sx <= x) {
sx *= 10;
}
while (sy <= y) {
sy *= 10;
}
return (int) (-sy * x - y + sx * y + x);
});
if (numsArr[0] == 0) {
return "0";
}
StringBuilder ret = new StringBuilder();
for (int num : numsArr) {
ret.append(num);
}
return ret.toString();
}
public String largestNumber1(int[] nums) {
PriorityQueue<String> heap = new PriorityQueue<>((x, y) -> (y + x).compareTo(x + y));
for(int x: nums) heap.offer(String.valueOf(x));
String res = "";
while(heap.size() > 0) res += heap.poll();
if(res.charAt(0) == '0') return "0";
return res;
}
}