用于在Java中对2D数组中的列进行反转的方法

时间:2014-02-24 19:06:13

标签: java arrays matrix

问题是2D array写一个方法来反转列。如果可能,请执行此操作in-place 我已经实现并且它工作正常。但它不是in-place。它使用auxillary storage,是否可以在不使用矩阵的情况下反转2D数组的列。

Here is my code:
public static int[][] reverseColumns(int[][] matrix){
        int rows=matrix.length;
        int cols=matrix[0].length;
        int temp=0;

        int[][] result= new int[rows][cols];

        for (int row=rows-1; row >=0; row--){
            for (int col=0;col<cols;col++){
                result[row][col]=matrix[temp][col];
            }
            temp++;
        }
        return result;
    }

  public static void print2DArray(int[][] result){
    for (int i=0; i < result.length;i++){
        System.out.println(Arrays.toString(result[i]));

    }
}

public static void main(String[] args)
    {
        int[][] matrix = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16}
            int[][] result = reverseColumns(matrix);
    print2DArray(result);       
    System.out.println()
        };

输出是:

[13, 14, 15, 16]
[9, 10, 11, 12]
[5, 6, 7, 8]
[1, 2, 3, 4]

3 个答案:

答案 0 :(得分:2)

我按照你的建议遵循了cols和row反转的语义:

   1 2 3                                     7 8 9
   4 5 6    _______column reversal_______    4 5 6 (vertically reversed) 
   7 8 9                                     1 2 3   



   1 2 3                                     3 2 1
   4 5 6    _______row reversal__________    6 5 4 (horizontally reversed) 
   7 8 9                                     9 8 7   

可以同时执行in-place: 对于horizontal (row reversal),它很简单。对于vertical (col reversal),它需要更多的理解。这里的方法是;举一个例子matrix并尝试按照步骤操作,您将理解

public static void reverseColumnsInPlace(int[][] matrix){
        for(int col = 0;col < matrix[0].length; col++){
            for(int row = 0; row < matrix.length/2; row++) {
                int temp = matrix[row][col];
                matrix[row][col] = matrix[matrix.length - row - 1][col];
                matrix[matrix.length - row - 1][col] = temp;
            }
    }
}

public static void reverseRowsInPlace(int[][] matrix){

    for(int row = 0; row < matrix.length; row++){
        for(int col = 0; col < matrix[row].length / 2; col++) {
            int temp = matrix[row][col];
            matrix[row][col] = matrix[row][matrix[row].length - col - 1];
            matrix[row][matrix[row].length - col - 1] = temp;
        }
    }
}

答案 1 :(得分:0)

诀窍是拥有一个计数器变量,比如row

然后只循环到数组的中间,并执行

tmp = matrix[row];
matrix[row]=matrix[rows-1-row];
matrix[rows-1-row]=tmp;

其中rows是总数。

我会让你弄清楚tmp的类型。

这只是使用一个恒定的辅助存储器,
因为tmp只是一个引用(Java中有4个字节)。

public static int[][] reverseColumns(int[][] matrix){
    int rows=matrix.length;
    int[] temp=null;
    for (int row=rows-1; row>=rows/2; row--){
        temp = matrix[row]; 
        matrix[row]=matrix[rows-1-row];
        matrix[rows-1-row] = temp;
    }
    return matrix;
}

答案 2 :(得分:-1)

这可以帮助您进行就地交流:

private void swap(int[][] A, int row1, int col1, int row2, int col2) {
    int tmp = A[row1][col1];
    A[row1][col1] = A[row2][col2];
    A[row2][col2] = tmp;
}