【问题标题】:Permute an array and saving each result to an ArrayList置换数组并将每个结果保存到 ArrayList
【发布时间】:2020-02-21 04:57:39
【问题描述】:

我需要排列一个数组并将每个排列保存在一个 arrayList 中,我使用的是递归方法,但它只会重复保存一个结果。

private static List<int[]> permutations = new ArrayList<int[]>();
int array[] = new int[]{1,2,3,4};

public static void permuteArray(int[] array) {
    permuteArray(array, 0);

}

private static void permuteArray(int[] array, int index) {
    if (index == array.length - 1) {
        permutations.add(array);
    }

    for (int i = index; i < array.length; i++) {

        int aux = array[index];
        array[index] = array[i];
        array[i] = aux;

        permuteArray(array, index + 1);

        aux = array[index];
        array[index] = array[i];
        array[i] = aux;

    }
}

【问题讨论】:

标签: java recursion permutation


【解决方案1】:

你可以这样做:

public class Permutation {
    public static void main(String[] args) {
        java.util.List<StringBuilder> result=new java.util.ArrayList<StringBuilder>();
        permute(java.util.Arrays.asList(1,2,3,4), 0,result);
        for(StringBuilder sb:result)
            System.out.println(sb.toString());
    }

    static void permute(java.util.List<Integer> arr, int k, java.util.List<StringBuilder> result){
        for(int i = k; i < arr.size(); i++){
            java.util.Collections.swap(arr, i, k);
            permute(arr, k+1,result);
            java.util.Collections.swap(arr, k, i);
        }
        if (k == arr.size() -1){
            result.add(new StringBuilder(java.util.Arrays.toString(arr.toArray())));      
        }
    }
}

输出:

[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 3, 2]
[1, 4, 2, 3]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 3, 1]
[2, 4, 1, 3]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 2, 3, 1]
[4, 2, 1, 3]
[4, 3, 2, 1]
[4, 3, 1, 2]
[4, 1, 3, 2]
[4, 1, 2, 3]

【讨论】:

  • 这将生成组合,而不是排列,或者?
【解决方案2】:

我发现我创建的这个实现非常有用,特别是因为它避免了像集合这样的大量对象的创建,它全部基于 int[] 数组。

顶部的方法执行所有递归繁重的工作,底部的三个 generate() 方法显示了示例调用。您可以使用 int[] 数组或使用字符串调用它,然后将其拆分为 char[] ...计算适用于整数和 char,您只需将它们转换回 char 以便之后输出。第一个 generate() 方法只是创建一个长度为 n 的 int[] 并为方便起见用 0, 1, ..., n-1 填充它。

要获得排列,只需遍历 int[][] 的第一个维度,第二个维度保存值。

public class Permutations {

    private int[][] permutations;
    private int resultIndex = 0;

    private void permutate(int[] resultCollector, int resultCollectorIndex, int[] permutableValues) {
        if (permutableValues.length > 0) {
            for (int i = 0; i < permutableValues.length; i++) {
                int[] nextResultCollector = Arrays.copyOf(resultCollector, resultCollector.length);
                nextResultCollector[resultCollectorIndex] = permutableValues[i];
                permutate(nextResultCollector, resultCollectorIndex + 1, reduce(permutableValues, i));
            }
        } else {
            System.arraycopy(resultCollector, 0, permutations[resultIndex], 0, resultCollector.length);
            resultIndex++;
        }
    }

    private int[] reduce(int[] d, int k) {
        int[] r = new int[d.length - 1];
        int ind = 0;
        for (int i = 0; i < d.length; i++) {
            if (i != k) {
                r[ind++] = d[i];
            }
        }
        return r;
    }

    public int[][] generate(int n) {
        int permutationCount = factorial(n);
        permutations = new int[permutationCount][n];
        int[] d = new int[n];
        for (int i = 0; i < n; i++) {
            d[i] = i;
        }
        return generate(d);
    }

    public int[][] generate(String input) {
        int[] d = new int[input.length()];
        for (int i = 0; i < input.length(); i++) {
            d[i] = input.charAt(i);
        }
        return generate(d);
    }

    public int[][] generate(int[] input) {
        int n = input.length;
        int permutationCount = factorial(n);
        permutations = new int[permutationCount][n];
        permutate(new int[n], 0, input);
        return permutations;
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2013-04-03
    • 2021-07-24
    相关资源
    最近更新 更多