0105-2020-LEETCODE-48- rotating image

I did not write it down.
Solution Source: https: //leetcode-cn.com/problems/rotate-image/solution/xuan-zhuan-tu-xiang-by-leetcode/
Code Source:
1.LEETCODE official solution to a problem first and then transposed reverse each line of output.

package Solution;

import org.junit.Test;

/**
 * @author pdzz
 * @create 2020-01-05 9:50
 */
public class Solution {

    /*

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

        原地旋转输入矩阵,使其变为:
        [
          [7,4,1],
          [8,5,2],
          [9,6,3]
        ]
        [0,0]->[0,2] [0,1]->[1,2] [0,2]->[2,2]
        [1,0]->[0,1] [1,1]->[1,1] [1,2]->[2,1]
        [2,0]->[0,0] [2,1]->[1,0] [2,2]->[2,0]
    */

    public void rotate(int[][] matrix) {
        //矩阵的转置
        int len = matrix.length;
        for (int i = 0; i < len; i++) {
            //不能写j=0的原因是,只需要旋转上半区就好。
            //如果都旋转就相当于旋转两次,最后就相当于没有转置。
            for (int j = i; j < len; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        //每一行逆序输出
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len / 2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][len - j - 1];//[0,0]->[0,2] [0,2]->[0,0] [0,1]->[0,1]
                matrix[i][len - j - 1] = temp;
            }
        }
    }
    @Test
    public void test1(){
        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
        rotate(matrix);
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println();
        }
    }
}

2 into a rectangular rotation
Solution Source: https: //leetcode-cn.com/problems/rotate-image/solution/xuan-zhuan-tu-xiang-by-leetcode/
using int [] temp = new int [ 4 ]; processed four directions, the key is to find out the coordinates of the current laws of each rotation and the next coordinate.
In fact, the following code:

int x = row;
row = col;
col = n - 1 - x; 
public void rotate(int[][] matrix) {
        /**
        *@Description 四个方向的旋转。使用一个int[] temp = new int[4];
        *@Param [matrix]
        *@Return void
        *@Author pdzz
        *@Date 2020/1/5
        *@Time 21:24
        */
        int n = matrix.length;
        for (int i = 0; i < n / 2 + n % 2; i++) {
            //行数从 0 到 n / 2 + n % 2;
            //列数从 0 到 n / 2;
            for (int j = 0; j < n / 2; j++) {
                int[] tmp = new int[4];
                int row = i;
                int col = j;
                for (int k = 0; k < 4; k++) {
                    tmp[k] = matrix[row][col];
                    int x = row;
                    row = col;
                    col = n - 1 - x;
                }
                for (int k = 0; k < 4; k++) {
                    matrix[row][col] = tmp[(k + 3) % 4];
                    int x = row;
                    row = col;
                    col = n - 1 - x;
                }
            }
        }
    }

3. The official explanations from LEETCODE solution of 2 Lite Source: https: //leetcode-cn.com/problems/rotate-image/solution/xuan-zhuan-tu-xiang-by-leetcode/

public void rotate2(int[][] matrix) {
        /**
        *@Description 其实是上一种方法的精简版,把两个次数为4的循环写出来。
        *@Param [matrix]
        *@Return void
        *@Author pdzz
        *@Date 2020/1/6
        *@Time 11:25
        */
        int n = matrix.length;
        for (int i = 0; i < (n + 1) / 2; i ++) {
            for (int j = 0; j < n / 2; j++) {
                /*
                * [0,0]     [0,2]
                *
                * [2,0]     [2,2]
                *
                * */
                //以[0,0]为例,首先将[0,0]的前一个[2,0]存入temp
                int temp = matrix[n - 1 - j][i];
                matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1];//[2,0] = [2,2]
                matrix[n - 1 - i][n - j - 1] = matrix[j][n - 1 -i];//[2,2] = [0,2]
                matrix[j][n - 1 - i] = matrix[i][j];//[0,2] = [0,0]
                matrix[i][j] = temp;//[0,0] = [2,0]
            }
        }
    }
Published 98 original articles · won praise 0 · Views 2209

Guess you like

Origin blog.csdn.net/weixin_43221993/article/details/103840964