有趣的算法-转置图像

╄→гoц情女王★ 提交于 2019-11-29 19:59:44


说明:

你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。

示例 1:

给定 matrix = 
[
  [1,2,3],  
  [4,5,6],  
  [7,8,9]   
],

原地旋转输入矩阵,使其变为:
[
  [7,4,1],                
  [8,5,2],  
  [9,6,3]   
]
示例 2:

给定 matrix =s
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

原地旋转输入矩阵,使其变为:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]

我发现,好像矩阵转置一下再每一行左右变换就可以了

[1,2],    [00 01]        翻转         [00 10] 
[3,4]     [10 11]                     [01 11]
然后每一行左右互换
after:
[3,1]    [10 00]                   
[4,2]    [11 01]
三维矩阵也是一样
给定 matrix = 
[
  [1,2,3],  [00 01 02]    翻转      [00 10 20]
  [4,5,6],  [10 11 12]              [01 11 21]
  [7,8,9]   [20 21 22]              [22 21 20]
],

然后开始写代码,可能是有点困傻了,写转置的时候我居然写成了这样

  for (int i=0;i<matrix.length;i++){
            for (int j=0;j<matrix.length;j++){         
                tem = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tem;
                //System.out.println(matrix[i][j] + " " + matrix[j][i]);
            }
        }

然后会发现整个矩阵好像就没变过,好吧不用好像,就没变过啊!

因为转完又给转回去了,所以我们看看是怎么转的,事实上,每次只需要翻转对角线的一边就可以了。

[1,2],    [00 01]        翻转         [00 10] 
[3,4]     [10 11]                     [01 11]
然后每一行左右互换
after:
[3,1]    [10 00]                   
[4,2]    [11 01]
三维矩阵也是一样
给定 matrix = 
[
  [1,2,3],  [00 01 02]    翻转      [00 10 20]
  [4,5,6],  [10 11 12]              [01 11 21]
  [7,8,9]   [20 21 22]              [22 21 20]
],

 

 

最后是我的解答:

class Solution {
    public void rotate(int[][] matrix) {
        int tem = 0;
        for (int i=0;i<matrix.length;i++){
            for (int j=i+1;j<matrix.length;j++){         //得出i=1的过程贼搞笑
                tem = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tem;
                //System.out.println(matrix[i][j] + " " + matrix[j][i]);
            }
        }
        for (int i=0;i<matrix.length;i++){
            for (int j=0;j<matrix.length/2;j++){
                tem = matrix[i][matrix.length-1-j];
                matrix[i][matrix.length-1-j] = matrix[i][j];
                matrix[i][j] = tem;
            }
        }
    }
}

完整的测试为

public class S4{//bywu大熊
    
    public void rotate(int[][] matrix) {
        S4 solution = new S4();
        int tem = 0;
        for (int i=0;i<matrix.length;i++){
            for (int j=i+1;j<matrix.length;j++){         //得出i=1的过程贼搞笑
                tem = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tem;
                //System.out.println(matrix[i][j] + " " + matrix[j][i]);
            }
        }
        for (int i=0;i<matrix.length;i++){
            for (int j=0;j<matrix.length/2;j++){
                tem = matrix[i][matrix.length-1-j];
                matrix[i][matrix.length-1-j] = matrix[i][j];
                matrix[i][j] = tem;
            }
        }
        System.out.println("after");
        solution.print_array(matrix);
    }
    public void print_array(int[][] arr){
        for (int i=0;i<4;i++){
            for (int j=0;j<4;j++){
                System.out.print("   "+arr[i][j]);
            }
            System.out.println("");
        }
    }
    public static void main(String[] args){
        S4 solution = new S4();
        int[][] matrix = {
              { 5, 1, 9,11},
              { 2, 4, 8,10},
              {13, 3, 6, 7},
              {15,14,12,16}
            };
        solution.print_array(matrix);
        solution.rotate(matrix);
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!