如何在Java中将矩阵逆时针旋转90度? [英] How do I rotate a matrix 90 degrees counterclockwise in java?

查看:128
本文介绍了如何在Java中将矩阵逆时针旋转90度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决破解编码面试"一书中的问题.问题之一要求我将矩阵顺时针旋转90度.现在,在试图巩固我对矩阵旋转的理解的同时,我尝试着一个新的问题:尝试将矩阵逆时针旋转(另一个方向)90度.

我试图遍历正方形矩阵的各层,即外层,一直迭代到内层,并旋转正方形"每一侧的所有索引.逐个.这基本上就是盖尔·拉克曼·麦克道威尔(Gayle Laakman McDowell)解决方案所实现的,但方向却是相反的.

  public static void rotationMatrix(int [] [] matrix){如果(matrix.length == 0){返回;}for(int i = 0; i  

我期望样品矩阵的结果

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

成为

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

但是我的代码导致了

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

我的代码中的缺陷/差异在哪里?

解决方案

如果绘制矩阵进行可视化,您会发现某些索引已关闭.例如,不要使用 matrix.length-1 ,而应在更新中使用bottom,因为图层正方形的大小会随着 i 的增加而减小.另一个错误是,在第二次更新中,您应该具有:

  matrix [j] [bottom] = matrix [bottom] [bottom-(j-top)]; 

代替:

  matrix [j] [bottom] = matrix [bottom] [j]; 

这是因为在图层的底部行中,索引从最后一列开始,然后向后移动到第一列. j-top 表示您在图层顶行的距离.绘制出矩阵后,我发现正确的更新如下:

  public static void main(String [] args){整数n = 5;int [] [] a =新的int [n] [n];for(int i = 0; i< n; i ++){for(int j = 0; j< n; j ++){a [i] [j] = i * n + j + 1;}}rotationMatrix(a);for(int i = 0; i< a.length; i ++){for(int j = 0; j< a [0] .length; j ++){System.out.printf(%3d",a [i] [j]);}System.out.println();}} 

  public static void rotationMatrix(int [] [] matrix){如果(matrix.length == 0){返回;}for(int i = 0; i  

输出:

  5 10 15 20 254 9 14 19 243 8 13 18 232 7 12 17 221 6 11 16 21 

I'm trying to go over the problems in the Cracking the Coding Interview book. One of the questions asks me to rotate a matrix 90 degrees clockwise. Now, while trying to solidify my understanding of matrix rotation, I tried to embark on a new problem: to try to rotate a matrix 90 degrees counterclockwise (the other direction).

I've tried to go through layers of a square matrix, the outer layer, iterating all the way into the inner layer and rotating all the indexes of each side of the "square" one by one. This basically is what Gayle Laakman McDowell's solution implemented, but the other direction.

public static void rotateMatrix(int[][] matrix) {
    if (matrix.length == 0) {
        return;
    }
    for (int i = 0; i < matrix.length / 2; i++) {
        int top = i;
        int bottom = matrix.length - 1 - i;
        for (int j = top; j < bottom; j++) {
            int temp = matrix[top][j];
            matrix[top][j] = matrix[j][matrix.length - 1 - j];
            matrix[j][matrix.length - 1 - j] = matrix[bottom][j];
            matrix[bottom][j] = matrix[j][matrix.length - 1 - bottom];
            matrix[j][matrix.length - 1 - bottom] = temp;
        }
    }
}

I expected the result of a sample matrix

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

to be

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

but my code resulted in

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

Where is the flaw/discrepancy in my code?

解决方案

If you draw out the matrix to visualize, you'll see that some of your indices are off. For example, instead of using matrix.length-1, you should use bottom in your updates because the size of the layer's square will decrease as i increases. Another error is that in your second update, you should have:

matrix[j][bottom] = matrix[bottom][bottom - (j - top)];

instead of:

matrix[j][bottom] = matrix[bottom][j];

This is because in the bottom row of the layer the indices start from the last column and move backward to the first column. j - top indicates how far along you are in the top row of your layer. After drawing out the matrix, I found the correct updates to be as follows:

public static void main(String[] args) {
    int n = 5;
    int[][] a = new int[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = i * n + j + 1;
        }
    }
    rotateMatrix(a);
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[0].length; j++) {
            System.out.printf("%3d", a[i][j]);
        }
        System.out.println();
    }
}

public static void rotateMatrix(int[][] matrix) {
    if (matrix.length == 0) {
        return;
    }
    for (int i = 0; i < matrix.length / 2; i++) {
        int top = i;
        int bottom = matrix.length - 1 - i;
        for (int j = top; j < bottom; j++) {
            int temp = matrix[top][j];
            matrix[top][j] = matrix[j][bottom];
            matrix[j][bottom] = matrix[bottom][bottom - (j - top)];
            matrix[bottom][bottom - (j - top)] = matrix[bottom - (j - top)][top];
            matrix[bottom - (j - top)][top] = temp;
        }
    }
}

Output:

5 10 15 20 25
4  9 14 19 24
3  8 13 18 23
2  7 12 17 22
1  6 11 16 21

这篇关于如何在Java中将矩阵逆时针旋转90度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆