Possible Duplicate:
How can I find all of the permutations consisting of 1 element from a variable number of arrays of variable length?
假设我有 n 个包含如下元素的数组
a1 -> e11,e12,e13
a2 -> e21,e22,e23,e24,e25
a3 -> e31,e32
a4 -> e41,e42,e43,e44
...
an -> en1,en2,en3,en4,en5,en6
我想从上面的每个数组中获取包含 n 个元素的所有可能的排列。
对于前任。
e11,e21,e31,e41.........,en1
e13,e25,e32,e41.........,en6
等等...
建议我一种有效的方法,如果可能的话,建议使用 Java 或 C 代码片段。
最佳答案
您可以使用以下伪代码:
获取下一个排列的函数:
function NextPermutation(int[] current)
{
current[0] = current[0] + 1;
int pointer = 0;
while(pointer <= n && current[pointer] == limit[pointer])
{
current[pointer] = 0;
pointer = pointer + 1;
current[pointer] = current[pointer] + 1;
}
}
预填充代码:
int[] limit = new int[n + 2];
int[] current = new int[n + 2];
limit[1] = a1.length;
...
limit[n] = an.length;
while(current[n+1] == 0)
{
Print(current);
NextPermutation(current);
}
当前
数组包含数组a1, a2, ... , an
的索引。
关于java - 查找包含可变数量元素的可变数量数组的排列的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14582060/