旋转矩阵90度

时间:2019-03-23 18:00:42

标签: java

我想过将矩阵旋转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];
                }
            }
        }

我想到遍历该行,创建一个临时变量,然后当我到达该列时,我将其替换为临时变量,循环应该继续进行。你怎么说?

2 个答案:

答案 0 :(得分:1)

提示:

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

array(3) {
  ["Amount"]=>
  string(4) "9.00"
  ["TruncatedAmount"]=>
  string(4) "8.67"
  ["OtherChargesAmount"]=>
  string(4) "8.67"
}

答案 1 :(得分:0)

我创建了一个算法,将矩阵向右旋转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]