【问题标题】:How do I rotate a matrix 90 degrees counterclockwise in java? [duplicate]如何在java中将矩阵逆时针旋转90度? [复制]
【发布时间】:2019-11-05 06:00:08
【问题描述】:

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

我尝试遍历方矩阵的层,即外层,一直迭代到内层,并一个一个地旋转“正方形”每一侧的所有索引。这基本上是 Gayle Laakman McDowell 的解决方案所实现的,但方向相反。

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]

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

【问题讨论】:

    标签: java arrays matrix multidimensional-array rotation


    【解决方案1】:

    如果您绘制矩阵以进行可视化,您会看到您的一些索引已关闭。例如,不要使用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) {
        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
    

    【讨论】:

      【解决方案2】:

      将矩阵旋转 90 度类似于转置。 [i][j] 的索引变为[j][i],但其一侧的索引变为等于 the length of the side, minus 1, minus the current index of the side。索引从 0 开始。

      int m = 5;
      int n = 4;
      
      int[][] arr1 = {
              {1, 2, 3, 4},
              {5, 6, 7, 8},
              {9, 10, 11, 12},
              {13, 14, 15, 16},
              {17, 18, 19, 20}};
      
      int[][] arr2 = new int[n][m];
      int[][] arr3 = new int[n][m];
      
      IntStream.range(0, m).forEach(i ->
              IntStream.range(0, n).forEach(j -> {
                  // turn matrix 90º clockwise ⟳
                  arr2[j][m - 1 - i] = arr1[i][j];
                  // turn matrix 90º anticlockwise ⟲
                  arr3[n - 1 - j][i] = arr1[i][j];
              }));
      

      // Output to the markdown table:
      String[] matrices = Stream.of(arr1, arr2, arr3)
              .map(arr2d -> Arrays.stream(arr2d)
                      .map(arr -> Arrays.stream(arr)
                              .mapToObj(i -> String.format("%2d", i))
                              .toArray())
                      .map(Arrays::toString)
                      .collect(Collectors.joining("<br>")))
              .toArray(String[]::new);
      
      System.out.println("| original matrix | turn matrix 90º ⟳ | turn matrix 90º ⟲ |");
      System.out.println("|---|---|---|");
      System.out.println("|<pre>" + String.join("</pre>|<pre>", matrices) + "</pre>|");
      
      original matrix turn matrix 90º ⟳ turn matrix 90º ⟲
      [ 1,  2,  3,  4]
      [ 5, 6, 7, 8]
      [ 9, 10, 11, 12]
      [13, 14, 15, 16]
      [17, 18, 19, 20]
      [17, 13,  9,  5,  1]
      [18, 14, 10, 6, 2]
      [19, 15, 11, 7, 3]
      [20, 16, 12, 8, 4]
      [ 4,  8, 12, 16, 20]
      [ 3, 7, 11, 15, 19]
      [ 2, 6, 10, 14, 18]
      [ 1, 5, 9, 13, 17]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-24
        • 1970-01-01
        • 2020-11-30
        • 2013-05-05
        • 2020-09-04
        • 2013-09-03
        • 1970-01-01
        相关资源
        最近更新 更多