Repository navigation
Expand file tree
/
Copy pathCode01_LargestNumber.java
More file actions
123 lines (112 loc) · 3.35 KB
/
Copy pathCode01_LargestNumber.java
File metadata and controls
123 lines (112 loc) · 3.35 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package class089;
import java.util.ArrayList;
import java.util.Arrays;
// 最大数
// 给定一组非负整数nums
// 重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数
// 测试链接 : https://leetcode.cn/problems/largest-number/
public class Code01_LargestNumber {
// strs中全是非空字符串,要把所有字符串拼接起来,形成字典序最小的结果
// 暴力方法
// 为了验证
// 生成所有可能的排列
// 其中选出字典序最小的结果
public static String way1(String[] strs) {
ArrayList<String> ans = new ArrayList<>();
f(strs, 0, ans);
ans.sort((a, b) -> a.compareTo(b));
return ans.get(0);
}
// 全排列代码,讲解038,常见经典递归过程解析
public static void f(String[] strs, int i, ArrayList<String> ans) {
if (i == strs.length) {
StringBuilder path = new StringBuilder();
for (String s : strs) {
path.append(s);
}
ans.add(path.toString());
} else {
for (int j = i; j < strs.length; j++) {
swap(strs, i, j);
f(strs, i + 1, ans);
swap(strs, i, j);
}
}
}
public static void swap(String[] strs, int i, int j) {
String tmp = strs[i];
strs[i] = strs[j];
strs[j] = tmp;
}
// strs中全是非空字符串,要把所有字符串拼接起来,形成字典序最小的结果
// 正式方法
// 时间复杂度O(n*logn)
public static String way2(String[] strs) {
Arrays.sort(strs, (a, b) -> (a + b).compareTo(b + a));
StringBuilder path = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
path.append(strs[i]);
}
return path.toString();
}
// 为了验证
// 生成长度1~n的随机字符串数组
public static String[] randomStringArray(int n, int m, int v) {
String[] ans = new String[(int) (Math.random() * n) + 1];
for (int i = 0; i < ans.length; i++) {
ans[i] = randomString(m, v);
}
return ans;
}
// 为了验证
// 生成长度1~m,字符种类有v种,随机字符串
public static String randomString(int m, int v) {
int len = (int) (Math.random() * m) + 1;
char[] ans = new char[len];
for (int i = 0; i < len; i++) {
ans[i] = (char) ('a' + (int) (Math.random() * v));
}
return String.valueOf(ans);
}
// 为了验证
// 对数器
public static void main(String[] args) {
int n = 8; // 数组中最多几个字符串
int m = 5; // 字符串长度最大多长
int v = 4; // 字符的种类有几种
int testTimes = 2000;
System.out.println("测试开始");
for (int i = 1; i <= testTimes; i++) {
String[] strs = randomStringArray(n, m, v);
String ans1 = way1(strs);
String ans2 = way2(strs);
if (!ans1.equals(ans2)) {
// 如果出错了
// 可以增加打印行为找到一组出错的例子
// 然后去debug
System.out.println("出错了!");
}
if (i % 100 == 0) {
System.out.println("测试到第" + i + "组");
}
}
System.out.println("测试结束");
}
// 测试链接 : https://leetcode.cn/problems/largest-number/
public static String largestNumber(int[] nums) {
int n = nums.length;
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
strs[i] = String.valueOf(nums[i]);
}
Arrays.sort(strs, (a, b) -> (b + a).compareTo(a + b));
if (strs[0].equals("0")) {
return "0";
}
StringBuilder path = new StringBuilder();
for (String s : strs) {
path.append(s);
}
return path.toString();
}
}