[英]Recursive Permutation With Multiple Lists
I'm curious how I can perform my permuteAndPrintValuesThreeLists_Iterative
method recursively... I know basic recursion for sorting arrays and performing binary searches, but I can't figure out how to make it my method recursive. 我很好奇我如何递归执行
permuteAndPrintValuesThreeLists_Iterative
方法...我知道排序数组和执行二进制搜索的基本递归,但是我不知道如何使它成为我的方法递归。
The reason I want to use recursion, is because I want to have the possibility to add more than 3 lists, without changing my method by adding another for loop. 我要使用递归的原因是,因为我希望可以添加3个以上的列表,而无需通过添加另一个for循环来更改我的方法。
Question : How do I write permuteAndPrintValuesThreeLists
method as a recursive
method ? 问题 :如何将
permuteAndPrintValuesThreeLists
方法编写为recursive
方法?
My output should be: 我的输出应为:
1 1 10 10 100 100
1 1 10 10 200 200
1 1 10 10 300 300
1 1 20 20 100 100
1 1 20 20 200 200
1 1 20 20 300 300
2 2 10 10 100 100
2 2 10 10 200 200
2 2 10 10 300 300
2 2 20 20 100 100
2 2 20 20 200 200
2 2 20 20 300 300
But it is: 但它是:
1 1 10 10 100 100
200 200
300 300
400 400
20 20 100 100
200 200
300 300
400 400
3 3 10 10 100 100
200 200
300 300
400 400
20 20 100 100
200 200
300 300
400 400
final class Problem {
public static void main(String[] args) {
Problem p = new Problem();
p.permuteAndPrintValuesThreeLists_Iterative();
}
private static List<int[]> l1;
private static List<int[]> l2;
private static List<int[]> l3;
private Problem() {
l1 = new ArrayList<>();
l1.add(new int[] { 1, 1 });
l1.add(new int[] { 2, 2 });
l2 = new ArrayList<>();
l2.add(new int[] { 10, 10 });
l2.add(new int[] { 20, 20 });
l3 = new ArrayList<>();
l3.add(new int[] { 100, 100 });
l3.add(new int[] { 200, 200 });
l3.add(new int[] { 300, 300 });
}
private static void permuteAndPrintValuesThreeLists_Iterative() {
for (int i = 0; i < l1.size(); i++) {
for (int j = 0; j < l2.size(); j++) {
for (int k = 0; k < l3.size(); k++) {
printArray(l1.get(i));
printArray(l2.get(j));
printArray(l3.get(k));
System.out.println();
}
}
}
}
private static void printArray(int[] a) {
for (int i : a) {
System.out.println(i + " ");
}
}
}
So far I knew I need to have a list that contains the 3 lists (in my case I added a HashMap). 到目前为止,我知道我需要一个包含3个列表的列表(在我的情况下,我添加了一个HashMap)。 I also have this solution method that partially solves the problem
我也有部分解决问题的解决方法
private static Map<Integer, List<int[]>> allLists = new HashMap<>();
private static void permuteAndPrintValuesThreeLists_Recursion(List<int[]> resultList, int mapIndex) {
if (mapIndex == allLists.size()) {
// Debug code
for (int[] arr : resultList)
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i] + " ");
resultList.clear();
System.out.println();
return;
}
for (int i = 0; i < allLists.get(mapIndex).size(); i++) {
int[] tmpArray = allLists.get(mapIndex).get(i);
resultList.add(tmpArray);
permuteAndPrintValuesThreeLists_Recursion(resultList, mapIndex + 1);
}
}
By adding a try catch block, I got the output that I printed in my question. 通过添加一个try catch块,我得到了打印在问题中的输出。 I use
set
to add the new values for the permutation. 我使用
set
为置换添加新值。
Now if I want a fourth list, this method still works. 现在,如果我想要第四个列表,则此方法仍然有效。
private static void solution_withRecursion(List<int[]> resultList, int mapIndex) {
if (mapIndex == allLists.size()) {
printListValues(resultList);
return;
}
for (int i = 0; i < allLists.get(mapIndex).size(); i++) {
int[] tmpArray = allLists.get(mapIndex).get(i);
try {
resultList.set(mapIndex, tmpArray);
} catch (IndexOutOfBoundsException e) {
resultList.add(tmpArray);
}
solution_withRecursion(resultList, mapIndex + 1);
}
}
private static void printListValues(List<int[]> list) {
for (int[] arr : list) {
for (int i : arr) {
System.out.print(i + " ");
}
}
System.out.println();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.