Square matrix rotation (48) solution

topic

You are given an N × Nimage represented by a matrix where each pixel is 4 bytes in size. Please design an algorithm to rotate the image by 90 degrees.

Can it be done without taking up extra memory space?

Example 1:

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

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

Example 2:

给定 matrix =
[
  [ 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]
]

analyze

The given matrix shape is a N x Nsquare matrix, so its width and height are known , and the rotation degree is Nrequired , then the array will be changed , and the filling method will also be changed .90横向排列竖向排列倒序填充

As shown in the picture:

insert image description here

After the conversion, the horizontal subscript becomes the vertical subscript in reverse order. The formula is:横向index = 竖向N-1-index

the code

It is very simple if you allow an empty matrix to accept, time complexity: O(n²)space complexity:O(2n)

    public void rotate(int[][] matrix) {
    
    
        int[][] result = new int[matrix.length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
    
    
            int now[] = matrix[i];
            // 计算旋转后下标
            int index = matrix.length-1-i;
            for (int j = 0; j < now.length; j++) {
    
    
                result[j][index] = now[j];
            }
        }
    }

The title also requires trying not to adapt to the extra space, so the temporary matrix cannot be used, and it needs to be rotated within the given matrix, that is 原地旋转; time complexity: O(n²)space complexity:O(1)

Rotating in situ will cover a part of the value, so the covered value also needs to be stored. If the cover adjustment is performed directly on the above line, too many cover values ​​will be generated, so it is necessary to adjust the value one by one to the correct position. Adjust from the first digit of each line.

insert image description here

code show as below:

    public void rotate(int[][] matrix) {
    
    
        int length = matrix.length;
        for (int i = 0; i < matrix.length/2; i++) {
    
    
            for (int j = i; j < matrix.length - (i + 1); j++) {
    
    
                int temp = matrix[i][j];
                matrix[i][j] = matrix[length - j-1][i];
                matrix[length - j -1][i] = matrix[length-i-1][length-j-1];
                matrix[length-i-1][length-j-1] = matrix[j][length-i-1];
                matrix[j][length-i-1] = temp;
            }
        }
    }

There is another way, 正方形旋转90°= 正方形水平反转+ 对角线反转; time complexity: O(n²)space complexity:O(1)

insert image description here

code show as below:

    public void rotate(int[][] matrix) {
    
    
        int n = matrix.length;
        // 水平翻转
        for (int i = 0; i < n / 2; ++i) {
    
    
            for (int j = 0; j < n; ++j) {
    
    
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - i - 1][j];
                matrix[n - i - 1][j] = temp;
            }
        }
        // 主对角线翻转
        for (int i = 0; i < n; ++i) {
    
    
            for (int j = 0; j < i; ++j) {
    
    
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }

Guess you like

Origin blog.csdn.net/AnNanDu/article/details/126740146