|
|
@ -1,9 +1,8 @@
|
|
|
|
package class09_13;
|
|
|
|
package class09_13;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.TreeSet;
|
|
|
|
|
|
|
|
|
|
|
|
public class Code01_LowestLexicography {
|
|
|
|
public class Code01_LowestLexicography {
|
|
|
|
|
|
|
|
|
|
|
@ -11,34 +10,41 @@ public class Code01_LowestLexicography {
|
|
|
|
if (strs == null || strs.length == 0) {
|
|
|
|
if (strs == null || strs.length == 0) {
|
|
|
|
return "";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArrayList<String> all = new ArrayList<>();
|
|
|
|
TreeSet<String> ans = process(strs);
|
|
|
|
HashSet<Integer> use = new HashSet<>();
|
|
|
|
return ans.size() == 0 ? "" : ans.first();
|
|
|
|
process(strs, use, "", all);
|
|
|
|
|
|
|
|
String lowest = all.get(0);
|
|
|
|
|
|
|
|
for (int i = 1; i < all.size(); i++) {
|
|
|
|
|
|
|
|
if (all.get(i).compareTo(lowest) < 0) {
|
|
|
|
|
|
|
|
lowest = all.get(i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return lowest;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// strs里放着所有的字符串
|
|
|
|
// strs中所有字符串全排列,返回所有可能的结果
|
|
|
|
// 已经使用过的字符串的下标,在use里登记了,不要再使用了
|
|
|
|
public static TreeSet<String> process(String[] strs) {
|
|
|
|
// 之前使用过的字符串,拼接成了-> path
|
|
|
|
TreeSet<String> ans = new TreeSet<>();
|
|
|
|
// 用all收集所有可能的拼接结果
|
|
|
|
if (strs.length == 0) {
|
|
|
|
public static void process(String[] strs, HashSet<Integer> use, String path, ArrayList<String> all) {
|
|
|
|
ans.add("");
|
|
|
|
if (use.size() == strs.length) {
|
|
|
|
return ans;
|
|
|
|
all.add(path);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for (int i = 0; i < strs.length; i++) {
|
|
|
|
for (int i = 0; i < strs.length; i++) {
|
|
|
|
if (!use.contains(i)) {
|
|
|
|
String first = strs[i];
|
|
|
|
use.add(i);
|
|
|
|
String[] nexts = removeIndexString(strs, i);
|
|
|
|
process(strs, use, path + strs[i], all);
|
|
|
|
TreeSet<String> next = process(nexts);
|
|
|
|
use.remove(i);
|
|
|
|
for (String cur : next) {
|
|
|
|
|
|
|
|
ans.add(first + cur);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// {"abc", "cks", "bct"}
|
|
|
|
|
|
|
|
// 0 1 2
|
|
|
|
|
|
|
|
// removeIndexString(arr , 1) -> {"abc", "bct"}
|
|
|
|
|
|
|
|
public static String[] removeIndexString(String[] arr, int index) {
|
|
|
|
|
|
|
|
int N = arr.length;
|
|
|
|
|
|
|
|
String[] ans = new String[N - 1];
|
|
|
|
|
|
|
|
int ansIndex = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
|
|
|
if (i != index) {
|
|
|
|
|
|
|
|
ans[ansIndex++] = arr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class MyComparator implements Comparator<String> {
|
|
|
|
public static class MyComparator implements Comparator<String> {
|
|
|
@ -91,13 +97,7 @@ public class Code01_LowestLexicography {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
int arrLen = 6;
|
|
|
|
int arrLen = 6;
|
|
|
|
int strLen = 5;
|
|
|
|
int strLen = 5;
|
|
|
|
int testTimes = 100000;
|
|
|
|
int testTimes = 10000;
|
|
|
|
String[] arr = generateRandomStringArray(arrLen, strLen);
|
|
|
|
|
|
|
|
System.out.println("先打印一个生成的字符串");
|
|
|
|
|
|
|
|
for (String str : arr) {
|
|
|
|
|
|
|
|
System.out.print(str + ",");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
|
|
|
System.out.println("test begin");
|
|
|
|
System.out.println("test begin");
|
|
|
|
for (int i = 0; i < testTimes; i++) {
|
|
|
|
for (int i = 0; i < testTimes; i++) {
|
|
|
|
String[] arr1 = generateRandomStringArray(arrLen, strLen);
|
|
|
|
String[] arr1 = generateRandomStringArray(arrLen, strLen);
|
|
|
|