【问题标题】:Java: Transpose matrix - Divide and conquerJava:转置矩阵 - 分而治之
【发布时间】:2017-11-01 22:00:00
【问题描述】:

我需要编写代码来转换转置矩阵,但我做不到,程序只替换了几个位置,其他位置保持不变。对这个问题有什么想法吗?

我需要使用任何矩阵 nxn

public static int [][] getTransposed(int matrix[][], int initRow, int endRow, int initColumn, int endColumn, int totalSize) {

    if (endRow - initRow <= 2 && endColumn - initColumn <= 2) {
        return invertPosition(matrix, initRow, endRow, initColumn, endColumn);
    } else {
        int mediumRow = (initRow + endRow) / 2;
        int mediumColumn = (initColumn + endColumn) / 2;

        getTransposed(matrix, initRow, mediumRow, initColumn, mediumColumn, totalSize);
        getTransposed(matrix, initRow, mediumRow, mediumColumn, endColumn, totalSize);
    }

    return matrix;
}

private static int [][] invertPosition(int matrix[][], int initRow, int endRow, int initColumn, int endColumn) {
    int temp;
    for (int r = initRow; r < endRow; r++) {
        for (int c = initColumn; c < endColumn; c++) {
                temp = matrix[r][c];
                matrix[r][c] = matrix[c][r];
                matrix[c][r] = temp;
        }
    }
    return matrix;
}

【问题讨论】:

  • 该代码看起来比完成这项任务要困难得多。至于问题,您正在从 getTransposed 函数返回值,但是当您递归调用它时,您不会将该值分配给任何东西。
  • 一眼看去,我错过了两个递归调用,涉及getTransposed(matrix, modiumRow, endRow, ...)(一个用于两个列范围)。一些简单的System.out.println(/* all row/col information*/); 语句可能会让您自己解决这个问题。如果没有,通常会感谢minimal reproducible example

标签: java matrix transpose divide-and-conquer


【解决方案1】:

我建议创建一种方法来查找 nxn Matrix 中给定 转置 >Java 通过利用可用的类,例如ArrayList。我会这样做:

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

public class MatrixTranspose{

    public static void main(String[] args) {

        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}}; // 3x3 square matrix
        int[][] matrix1 = {{1,2}}; // one row
        int[][] matrix2 = {{1},{4},{7}}; // one column
        int[][] matrix3 = {{1,2,3},{4,5,6}}; // 2x3 matrix
        int[][] matrix4 = {{1,2},{3,4},{5,6},{7,9}}; // 4x2 matrix

        // testing
        printTranspose(transpose(matrix));
        printTranspose(transpose(matrix1));
        printTranspose(transpose(matrix2));
        printTranspose(transpose(matrix3));
        printTranspose(transpose(matrix4));     
    }

    public static void printTranspose(int [][] transpose){
        for(int i=0; i<transpose.length ; i++, System.out.println()){
            for(int j=0; j<transpose[0].length; j++){
                System.out.print(transpose[i][j] + "\t");
            }
        }
        System.out.println("-------------------------");
    }

    public static int[][] transpose(int[][] matrix){
        // ArrayList to collect every column 
        ArrayList<Integer> oneColumn = new ArrayList<Integer>();
        // ArrayList to collect all Columns
        List<ArrayList<Integer>> columns = new ArrayList<ArrayList<Integer>>();
        // take the dimension of the array
        int rs = matrix.length;
        int cols = matrix[0].length; 

        // start collecting the elements
        for(int i=0; i<rs ; i++){
            for(int j=0; j<cols; j++){
                oneColumn.add(matrix[i][j]);
            }
            columns.add(oneColumn);
            oneColumn = new ArrayList<Integer>();
        }

        // create array for the final result (transpose)
        int[][] result = new int[cols][rs];

        // start converting the columns to rows and collect them
        for(int i=0; i<cols ; i++){
            for(int j=0; j<rs; j++){
                result[i][j] = columns.get(j).get(i);
            }
        }

        return result;  
    }
}

输出

1   4   7   
2   5   8   
3   6   9   
-------------------------
1   
2   
-------------------------
1   4   7   
-------------------------
1   4   
2   5   
3   6   
-------------------------
1   4   6   8   
2   5   7   9   
-------------------------

【讨论】:

  • 我需要分治法,这是必须的。不过谢谢。
猜你喜欢
  • 2016-02-24
  • 2012-03-04
  • 2020-04-11
  • 2017-12-05
  • 2017-07-16
  • 1970-01-01
  • 2011-06-18
  • 2016-11-17
  • 1970-01-01
相关资源
最近更新 更多