在Java中生成几个列表的所有排列

It'*_*sMe 0 java math permutation pseudocode discrete-mathematics

我有n清单,例如:

L_1 = [a_11, a_12, ...]
L_2 = [a_21, a_22, ...]
...
L_n = [a_n1, a_n2, ...]
Run Code Online (Sandbox Code Playgroud)

其中i次列表具有k_i的元素。现在,我想生成所有n-elements列表,其中ith元素来自L_i,我的意思是:

[a_11, a_21, ..., a_n1]
[a_11, a_21, ..., a_n2]
...
[a_11, a_22, ..., a_n1]
[a_11, a_22, ..., a_n2]
...
[a_12, a_21, ..., a_n1]
[a_12, a_21, ..., a_n2]
...
[a_12, a_22, ..., a_n1]
[a_12, a_22, ..., a_n2]
...
Run Code Online (Sandbox Code Playgroud)

列表总数应等于k_1*k_2*...k_n。您能描述这种算法的伪代码还是使用Java代码?当列表数量被硬编码时,我可以使用嵌套的for循环来完成此操作,但是当n在运行时可自定义时,我将被完全阻止。

It'*_*sMe 5

好的,我实现了该算法。

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PermutationGenerator {
    private List<List<Integer>> result;
    private List<List<Integer>> data;

    public List<List<Integer>> permutate(List<List<Integer>> data) {
        this.data = data;
        this.result = Lists.newArrayList();

        List<Integer> integers = new ArrayList<Integer>(Collections.nCopies(data.size(), 0));
        foo(0, data.size() - 1, integers);
        return result;
    }

    private void foo(Integer index, Integer maxIndex, List<Integer> output) {
        List<Integer> list = data.get(index);
        for (int i = 0; i < list.size(); i++) {
            output.set(index, list.get(i));
            if (index == maxIndex) {
                result.add(Lists.newArrayList(output));
            } else {
                foo(index + 1, maxIndex, output);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

测试类别:

import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class PermutationGeneratorTest {
    @Test
    public void test() throws Exception {
        // given
        PermutationGenerator pg = new PermutationGenerator();

        List<Integer> list1 = Lists.newArrayList(1, 2, 3);
        List<Integer> list2 = Lists.newArrayList(4, 5);
        List<Integer> list3 = Lists.newArrayList(6, 7, 8, 9);

        List<List<Integer>> input = Lists.newArrayList(list1, list2, list3);

        // when
        List<List<Integer>> output = pg.permutate(input);

        // then
        print(output);
    }

    private void print(List<List<Integer>> output) {
        for (List<Integer> list : output) {
            System.out.println(Arrays.toString(list.toArray()));
        }
        System.out.println("TOTAL: " + output.size());
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

[1, 4, 6]
[1, 4, 7]
[1, 4, 8]
[1, 4, 9]
[1, 5, 6]
[1, 5, 7]
[1, 5, 8]
[1, 5, 9]
[2, 4, 6]
[2, 4, 7]
[2, 4, 8]
[2, 4, 9]
[2, 5, 6]
[2, 5, 7]
[2, 5, 8]
[2, 5, 9]
[3, 4, 6]
[3, 4, 7]
[3, 4, 8]
[3, 4, 9]
[3, 5, 6]
[3, 5, 7]
[3, 5, 8]
[3, 5, 9]
TOTAL: 24
Run Code Online (Sandbox Code Playgroud)