@NullPointerException我今天早些时候的意思是,应用于给定URL的算法几乎是一个暴力算法,用于生成N个符号集中长度为6的所有组合。它的代码非常简单:
private static List<List<Integer>> bruteForce(List<Integer> numbers) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> current;
for (int position1 = 0; position1 < numbers.size(); position1++) {
for (int position2 = position1 + 1; position2 < numbers.size(); position2++) {
for (int position3 = position2 + 1; position3 < numbers.size(); position3++) {
for (int position4 = position3 + 1; position4 < numbers.size(); position4++) {
for (int position5 = position4 + 1; position5 < numbers.size(); position5++) {
for (int position6 = position5 + 1; position6 < numbers.size(); position6++) {
current = new ArrayList<>();
current.add(numbers.get(position1));
current.add(numbers.get(position2));
current.add(numbers.get(position3));
current.add(numbers.get(position4));
current.add(numbers.get(position5));
current.add(numbers.get(position6));
result.add(current);
}
}
}
}
}
}
return result;
}
还有一些用例://请记住它适用于array\u length>=6.
public static void main(String[] args) {
List<Integer> numbers;
System.out.println("N = 6, resulting in 1 element in the list.");
numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6);}};
bruteForce(numbers).forEach(System.out::println);
System.out.println("N = 7, resulting in 7 element in the list.");
numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7);}};
bruteForce(numbers).forEach(System.out::println);
System.out.println("N = 8, resulting in 28 element in the list.");
numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7); add(8);}};
bruteForce(numbers).forEach(System.out::println);
System.out.println("N = 9, resulting in 84 element in the list.");
numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7); add(8); add(9);}};
bruteForce(numbers).forEach(System.out::println);
}
N = 6, resulting in 1 element in the list.
[1, 2, 3, 4, 5, 6]
N = 7, resulting in 7 element in the list.
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 7]
[1, 2, 3, 4, 6, 7]
[1, 2, 3, 5, 6, 7]
[1, 2, 4, 5, 6, 7]
[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
[2, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8]
[3, 4, 5, 6, 7, 9]
[3, 4, 5, 6, 8, 9]
[3, 4, 5, 7, 8, 9]
[3, 4, 6, 7, 8, 9]
[3, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
请记住,输出中使用的数字来自
List<Integer> numbers = ...
所以你可以玩一玩,进一步测试。我还建议在这个例子中使用有序集而不是列表。
numbers = new ArrayList<Integer>() {{ add(11); add(22); add(33); add(44); add(45); add(46); add(47);}};
bruteForce(numbers).forEach(System.out::println);
印刷品:
[11, 22, 33, 44, 45, 46]
[11, 22, 33, 44, 45, 47]
[11, 22, 33, 44, 46, 47]
[11, 22, 33, 45, 46, 47]
[11, 22, 44, 45, 46, 47]
[11, 33, 44, 45, 46, 47]
[22, 33, 44, 45, 46, 47]
我将为您添加非常相同的方法的递归实现,该方法也将以基(子集长度)作为输入参数。
private static List<List<Integer>> bruteForceRecursive(List<Integer> numbers, List<Integer> indexes, int base) {
List<List<Integer>> result = new ArrayList<>();
if (indexes.size() == base) {
List<Integer> list = new ArrayList<>();
indexes.forEach(x -> list.add(numbers.get(x)));
result.add(list);
return result;
}
for (int i = indexes.isEmpty() ? 0 : indexes.get(indexes.size() - 1) + 1; i < numbers.size(); i++) {
indexes.add(i);
result.addAll(bruteForceRecursive(numbers, indexes, base));
indexes.remove(indexes.size() - 1);
}
return result;
}
然后是用法示例:
public static void main(String[] args) {
List<Integer> numbers;
numbers = new ArrayList<Integer>() {{ add(11); add(22); add(33); add(44); add(45); add(46); add(47);}};
bruteForceRecursive(numbers, new ArrayList<>(), 2).forEach(System.out::println);
bruteForceRecursive(numbers, new ArrayList<>(), 4).forEach(System.out::println);
bruteForceRecursive(numbers, new ArrayList<>(), 6).forEach(System.out::println);
}
打印:
[11, 22]
[11, 33]
[11, 44]
[11, 45]
[11, 46]
[11, 47]
[22, 33]
[22, 44]
[22, 45]
[22, 46]
[22, 47]
[33, 44]
[33, 45]
[33, 46]
[33, 47]
[44, 45]
[44, 46]
[44, 47]
[45, 46]
[45, 47]
[46, 47]
[11, 22, 33, 44]
[11, 22, 33, 45]
[11, 22, 33, 46]
[11, 22, 33, 47]
[11, 22, 44, 45]
[11, 22, 44, 46]
[11, 22, 44, 47]
[11, 22, 45, 46]
[11, 22, 45, 47]
[11, 22, 46, 47]
[11, 33, 44, 45]
[11, 33, 44, 46]
[11, 33, 44, 47]
[11, 33, 45, 46]
[11, 33, 45, 47]
[11, 33, 46, 47]
[11, 44, 45, 46]
[11, 44, 45, 47]
[11, 44, 46, 47]
[11, 45, 46, 47]
[22, 33, 44, 45]
[22, 33, 44, 46]
[22, 33, 44, 47]
[22, 33, 45, 46]
[22, 33, 45, 47]
[22, 33, 46, 47]
[22, 44, 45, 46]
[22, 44, 45, 47]
[22, 44, 46, 47]
[22, 45, 46, 47]
[33, 44, 45, 46]
[33, 44, 45, 47]
[33, 44, 46, 47]
[33, 45, 46, 47]
[44, 45, 46, 47]
[11, 22, 33, 44, 45, 46]
[11, 22, 33, 44, 45, 47]
[11, 22, 33, 44, 46, 47]
[11, 22, 33, 45, 46, 47]
[11, 22, 44, 45, 46, 47]
[11, 33, 44, 45, 46, 47]
[22, 33, 44, 45, 46, 47]