-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution45.java
More file actions
64 lines (53 loc) · 1.57 KB
/
Solution45.java
File metadata and controls
64 lines (53 loc) · 1.57 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public String minNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
String[] strs = new String[nums.length];
for (int i = 0; i < strs.length; i++) {
strs[i] = nums[i] + "";
}
fastSort(strs, 0, nums.length - 1);
StringBuilder sb = new StringBuilder();
for (String s : strs) {
sb.append(s);
}
return sb.toString();
}
private void fastSort(String[] strs, int l, int r) {
if (l >= r) {
return;
}
int i = l;
int j = r;
String tmp = strs[i];
while (i < j) {
while (true) {
String a = strs[j] + strs[l];
String b = strs[l] + strs[j];
if (a.compareTo(b) >= 0 && i < j) {
j--;
} else {
break;
}
}
while (true) {
String a = strs[i] + strs[l];
String b = strs[l] + strs[i];
if (a.compareTo(b) <= 0 && i < j) {
i++;
} else {
break;
}
}
tmp = strs[i];
strs[i] = strs[j];
strs[j] = tmp;
}
strs[i] = strs[l];
strs[l] = tmp;
fastSort(strs, l, i - 1);
fastSort(strs, i + 1, r);
}
}
// https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/solution/mian-shi-ti-45-ba-shu-zu-pai-cheng-zui-xiao-de-s-4/