旋转矩阵90度

问题描述 投票:2回答:2

我想将矩阵旋转90度,我还想把行转到列和列到行,我想过先将外侧旋转,内部和中间保持相同,因为它应该是5x5。无论如何,我不明白如何正确地做到这一点。

static int[][] multi = {
            { 3, 4, 5, 6, 7  }, 
            { 5, 4, 5, 6, 7 },
            { 6, 4, 5, 6, 7 },
            { 8, 4, 5, 6 ,7 },
            { 8, 4 ,5 ,6 ,7 } 
            }; 

    public static void Rotate_90_Degrees() {
            int temp = 0;
            for(int i = 0; i < 5; i++) {
                multi[i][0] = temp;
                for(int j = 0; j < 5; j++) {
                    temp = multi[0][j];
                }
            }
        }

我想通过行,制作一个临时变量,然后当我到达列时,我将用临时替换它,循环应该继续。你说什么?

java
2个回答
1
投票

暗示:

如果您想要就地执行旋转,您会注意到数据移动是四向交换,如:

M[i,j] -> M[n+1-j,i] -> M[n+1-i,n+1-j] -> M[j,n+1-i] -> M[i,j]

0
投票

我创建了一个algortihm,将矩阵向右旋转90°。如果你想要向左旋转,你可以简单地向右旋转3次(如果你当然不关心性能:))。该算法采用MxN矩阵。如果您只需要旋转NxN矩阵,那么您可以在原地进行。为简单起见,我没有在算法中包含这种情况。

我使用String作为矩阵基元类型,以便我们可以更好地看到输出单元格。当然,你可以用int作为基本类型做同样的事情。

import java.util.Arrays;

public class YouSpinMyHeadRightRound
{
    /**
     * Rotates the matrix by 90 degrees. Input needs to
     * be a "m x n" matrix.
     */
    public static String[][] rotateRightBy90Degrees(String[][] inputMatrix)
    {
        int rows, columns;
        rows = inputMatrix.length;
        columns = inputMatrix[0].length;

        int outputRows, outputColumns;
        outputRows = columns;
        outputColumns = rows;
        String[][] output = new String[outputRows][outputColumns];

        // fill the output matrix
        for (int i = 0; i < outputColumns; i++)
        {
            for (int j = 0; j < outputRows; j++)
            {
                output[j][outputColumns - 1 - i] = inputMatrix[i][j];
            }
        }
        return output;
    }

    /**
     * Prints the matrix to console.
     */
    public static void printMatrixToConsole(String[][] input)
    {
        for (int i = 0; i < input.length; i++)
        {
            System.out.println(Arrays.toString(input[i]));
        }
    }

    /*
     * For testing purposes!
     */
    public static void main(String[] args)
    {
        String[][] matrixA = new String[][] {{"00", "01", "02", "03"},
                {"10", "11", "12", "13"}, {"20", "21", "22", "23"}};

        String[][] rotated90 = YouSpinMyHeadRightRound
            .rotateRightBy90Degrees(matrixA);
        String[][] rotated180 = YouSpinMyHeadRightRound
            .rotateRightBy90Degrees(rotated90);
        String[][] rotated270 = YouSpinMyHeadRightRound
            .rotateRightBy90Degrees(rotated180);
        String[][] rotated360 = YouSpinMyHeadRightRound
            .rotateRightBy90Degrees(rotated270);

        System.out.println("Initial matrix: ");
        YouSpinMyHeadRightRound.printMatrixToConsole(matrixA);
        System.out.println();

        System.out.println("90° to the right:");
        YouSpinMyHeadRightRound.printMatrixToConsole(rotated90);
        System.out.println("180° to the right:");
        YouSpinMyHeadRightRound.printMatrixToConsole(rotated180);
        System.out.println("270° to the right:");
        YouSpinMyHeadRightRound.printMatrixToConsole(rotated270);
        System.out.println("360° to the right:");
        YouSpinMyHeadRightRound.printMatrixToConsole(rotated360);
        // the 360° matrix matches with matrixA
    }
}

输出是:

Initial matrix: 
[00, 01, 02, 03]
[10, 11, 12, 13]
[20, 21, 22, 23]

90° to the right:
[20, 10, 00]
[21, 11, 01]
[22, 12, 02]
[23, 13, 03]

180° to the right:
[23, 22, 21, 20]
[13, 12, 11, 10]
[03, 02, 01, 00]

270° to the right:
[03, 13, 23]
[02, 12, 22]
[01, 11, 21]
[00, 10, 20]

360° to the right:
[00, 01, 02, 03]
[10, 11, 12, 13]
[20, 21, 22, 23]
© www.soinside.com 2019 - 2024. All rights reserved.