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

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

问题描述

我正在尝试重新审视 Cracking the Coding Interview 一书中的问题.其中一个问题要求我将矩阵顺时针旋转 90 度.现在,在试图巩固我对矩阵旋转的理解的同时,我尝试着手解决一个新问题:尝试将矩阵逆时针旋转 90 度(另一个方向).

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).

我试图遍历方阵的层,外层,一直迭代到内层,并旋转正方形"每一边的所有索引.逐个.这基本上是 Gayle Laakman McDowell 的解决方案所实施的,但另一个方向.

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;
        }
    }
}

我期望样本矩阵的结果

[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]

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

Where is the flaw/discrepancy in my code?

推荐答案

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

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)];

代替:

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

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

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;
        }
    }
}

输出:

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天全站免登陆