-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179.largest-number.java
More file actions
34 lines (31 loc) · 996 Bytes
/
Copy path179.largest-number.java
File metadata and controls
34 lines (31 loc) · 996 Bytes
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
import java.util.PriorityQueue;
/*
* @lc app=leetcode id=179 lang=java
*
* [179] Largest Number
*/
// @lc code=start
class Solution {
public String largestNumber(int[] nums) {
// 排序的规则是,如果ab > ba 则a排在前面
// 因为是greater function,反过来比就是从大到小
PriorityQueue<Integer> numPQ = new PriorityQueue<Integer>((a, b) -> {
String intA = a.toString(), intB = b.toString();
return (intB + intA).compareTo(intA + intB);
});
for (int num: nums) {
numPQ.add(num);
}
StringBuilder resBuilder = new StringBuilder();
while (!numPQ.isEmpty()) {
int currNum = numPQ.poll();
// 跳过所有leading 0
if (!numPQ.isEmpty() && resBuilder.length() == 0 && currNum == 0) {
continue;
}
resBuilder.append(currNum);
}
return resBuilder.toString();
}
}
// @lc code=end