【问题标题】:Return all combinations from the matrix in Java [closed]从Java中的矩阵返回所有组合[关闭]
【发布时间】:2017-01-03 22:26:24
【问题描述】:

我见过这个问题的不同版本,但从来没有一个好的答案。我有一个 MXN 数组,想要返回 M 大小的所有可能组合。让我举个例子,有一个 3X3 数组。结果应该是 27 种组合。我在这里尝试递归方法,但到目前为止没有运气。

【问题讨论】:

  • 请发布您的代码,并解释您得到了什么以及为什么不正常。
  • 你试过实现它了吗?
  • 为什么一个 3x3 数组有 27 种组合?例如。如果每个数组元素可以有 2 个值,则将有 2^9 = 512 个排列。如果每个数组元素可以有 10 个值,则将有 10^9 = 1,000,000,000 个排列。你怎么只得到 27 种组合?
  • 或者您的意思是在 3 个插槽中生成所有 27 个 排列 的 3 个值?例如。 AAA, AAB, AAC, ABA, ..., CCB, CCC?如果是这样,那与 3×3 矩阵有什么关系?此外,这个问题有很多答案。
  • 我猜他希望每行有 M 行和 N 个值,并获得所有可能的 M 元素向量,这些向量是元素的每种可能组合,每行一个元素。这样,3x3 矩阵将给出 27 个 3 元素向量,2x4 将给出 16 个 2 元素向量,5x1 将给出 1 个 5 元素向量。

标签: java matrix combinations


【解决方案1】:

如果这个程序可以帮助你,请尝试。我用过这个矩阵

   int[][] matrix = {{1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}};

输出是 27 种组合,如下所示。

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

public class Main {
    private static List<int[]> combine(int[][] matrix) {
        int sizeArray[] = new int[matrix.length];
        int counterArray[] = new int[matrix.length];
        int total = 1;
        for (int i = 0; i < matrix.length; ++i) {
            sizeArray[i] = matrix[i].length;
            total *= matrix[i].length;
        }
        List<int[]> list = new ArrayList<>(total);
        StringBuilder sb;
        for (int count = total; count > 0; --count) {
            sb = new StringBuilder();
            for (int i = 0; i < matrix.length; ++i) {
                sb.append(matrix[i][counterArray[i]]);
            }
            int tmpi[] = new int[sb.toString().length()];
            for (int tmp = 0; tmp < sb.toString().length(); tmp++) {
                tmpi[tmp] = Integer.parseInt("" + sb.toString().toCharArray()[tmp]);
            }
            list.add(tmpi);
            for (int incIndex = matrix.length - 1; incIndex >= 0; --incIndex) {
                if (counterArray[incIndex] + 1 < sizeArray[incIndex]) {
                    ++counterArray[incIndex];
                    break;
                }
                counterArray[incIndex] = 0;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}};
        int i = 0;
        for (int[] c : (combine(matrix))) {
            System.out.println(Arrays.toString(c));
            i++;
        }
        System.out.println(i);
    }
}

测试

[1, 4, 7]
[1, 4, 8]
[1, 4, 9]
[1, 5, 7]
[1, 5, 8]
[1, 5, 9]
[1, 6, 7]
[1, 6, 8]
[1, 6, 9]
[2, 4, 7]
[2, 4, 8]
[2, 4, 9]
[2, 5, 7]
[2, 5, 8]
[2, 5, 9]
[2, 6, 7]
[2, 6, 8]
[2, 6, 9]
[3, 4, 7]
[3, 4, 8]
[3, 4, 9]
[3, 5, 7]
[3, 5, 8]
[3, 5, 9]
[3, 6, 7]
[3, 6, 8]
[3, 6, 9]
27

【讨论】:

    【解决方案2】:

    递归版本:

    import java.util.Arrays;
    
    public class Matrix  {
        private static int counter = 0;
    
        private static void combin2(int depth, int[][]matrix, int[] output)
        {
            int[] row = matrix[depth];
    
            if(depth == 0) {
                counter = 0;
                output = new int[matrix.length];
                System.out.println("matrix length: " + matrix.length);
            }
    
            for(int i=0; i<row.length; i++) {
                output[depth] = row[i];
    
                if(depth == (matrix.length-1)) {
                    //print the combination
                    System.out.println(Arrays.toString(output));
                    counter++;
                } else {
                    //recursively generate the combination
                    combin2(depth+1, matrix, output);
                }
            }
        }
    
        public static void main(String[] args) {
            int[][] matrix = new int[][] {{1, 2, 3, 4}, {5, 6, 7, 8}};
            combin2(0, matrix, null);
            System.out.println("counter = " + counter);
            System.out.println("");
    
            matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
            combin2(0, matrix, null);
            System.out.println("counter = " + counter);
            System.out.println("");
    
            matrix = new int[][]{{1, 2, 3}, {4,5,6}, {7,8,9}, {10,11,12}};
            combin2(0, matrix, null);
            System.out.println("counter = " + counter);
            System.out.println("");
        }
    }
    

    输出:

    matrix length: 2
    [1, 5]
    [1, 6]
    [1, 7]
    [1, 8]
    [2, 5]
    [2, 6]
    [2, 7]
    [2, 8]
    [3, 5]
    [3, 6]
    [3, 7]
    [3, 8]
    [4, 5]
    [4, 6]
    [4, 7]
    [4, 8]
    counter = 16
    
    matrix length: 3
    [1, 4, 7]
    [1, 4, 8]
    [1, 4, 9]
    [1, 5, 7]
    [1, 5, 8]
    [1, 5, 9]
    [1, 6, 7]
    [1, 6, 8]
    [1, 6, 9]
    [2, 4, 7]
    [2, 4, 8]
    [2, 4, 9]
    [2, 5, 7]
    [2, 5, 8]
    [2, 5, 9]
    [2, 6, 7]
    [2, 6, 8]
    [2, 6, 9]
    [3, 4, 7]
    [3, 4, 8]
    [3, 4, 9]
    [3, 5, 7]
    [3, 5, 8]
    [3, 5, 9]
    [3, 6, 7]
    [3, 6, 8]
    [3, 6, 9]
    counter = 27
    
    matrix length: 4
    [1, 4, 7, 10]
    [1, 4, 7, 11]
    [1, 4, 7, 12]
    [1, 4, 8, 10]
    [1, 4, 8, 11]
    [1, 4, 8, 12]
    [1, 4, 9, 10]
    [1, 4, 9, 11]
    [1, 4, 9, 12]
    [1, 5, 7, 10]
    [1, 5, 7, 11]
    [1, 5, 7, 12]
    [1, 5, 8, 10]
    [1, 5, 8, 11]
    [1, 5, 8, 12]
    [1, 5, 9, 10]
    [1, 5, 9, 11]
    [1, 5, 9, 12]
    [1, 6, 7, 10]
    [1, 6, 7, 11]
    [1, 6, 7, 12]
    [1, 6, 8, 10]
    [1, 6, 8, 11]
    [1, 6, 8, 12]
    [1, 6, 9, 10]
    [1, 6, 9, 11]
    [1, 6, 9, 12]
    [2, 4, 7, 10]
    [2, 4, 7, 11]
    [2, 4, 7, 12]
    [2, 4, 8, 10]
    [2, 4, 8, 11]
    [2, 4, 8, 12]
    [2, 4, 9, 10]
    [2, 4, 9, 11]
    [2, 4, 9, 12]
    [2, 5, 7, 10]
    [2, 5, 7, 11]
    [2, 5, 7, 12]
    [2, 5, 8, 10]
    [2, 5, 8, 11]
    [2, 5, 8, 12]
    [2, 5, 9, 10]
    [2, 5, 9, 11]
    [2, 5, 9, 12]
    [2, 6, 7, 10]
    [2, 6, 7, 11]
    [2, 6, 7, 12]
    [2, 6, 8, 10]
    [2, 6, 8, 11]
    [2, 6, 8, 12]
    [2, 6, 9, 10]
    [2, 6, 9, 11]
    [2, 6, 9, 12]
    [3, 4, 7, 10]
    [3, 4, 7, 11]
    [3, 4, 7, 12]
    [3, 4, 8, 10]
    [3, 4, 8, 11]
    [3, 4, 8, 12]
    [3, 4, 9, 10]
    [3, 4, 9, 11]
    [3, 4, 9, 12]
    [3, 5, 7, 10]
    [3, 5, 7, 11]
    [3, 5, 7, 12]
    [3, 5, 8, 10]
    [3, 5, 8, 11]
    [3, 5, 8, 12]
    [3, 5, 9, 10]
    [3, 5, 9, 11]
    [3, 5, 9, 12]
    [3, 6, 7, 10]
    [3, 6, 7, 11]
    [3, 6, 7, 12]
    [3, 6, 8, 10]
    [3, 6, 8, 11]
    [3, 6, 8, 12]
    [3, 6, 9, 10]
    [3, 6, 9, 11]
    [3, 6, 9, 12]
    counter = 81
    

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      相关资源
      最近更新 更多