Simple Two-Dimensional Matrix Rotate Image of java Algorithms

Reprinted from: http://blog.csdn.net/ylyg050518/article/details/48523857

Today we're still going to analyze an array operation-related algorithm It's about two-dimensional array rotation.

Problem description

original text

You are given an n × n 2D matrix representing an image. 
Rotate the image by 90 degrees (clockwise). 
Follow up: Could you do this in-place?

To give an image represented by a two-dimensional matrix of n * n, rotate the image clockwise by 90 degrees.  
For example, give a 3*3 matrix.

 [1,2,3 ] 
 [4,5,6 ] 
 [7,8,9 ] 

After rotating 90 degrees clockwise,

 [7,4,1 ] 
 [8,5,2 ] 
 [9,6,3 ] 
]

Thinking analysis

_First of all, the easiest thing to think about is to create a new n * n matrix to store the results, and then the corresponding rows of the original matrix can be stored in the corresponding columns of the result matrix. After a simple analysis, it can be found that the element of position (i,j) i n the original matrix should be (j,n-1-i) in the result matrix. With the corresponding relationship, we can quickly write the following code:

/*
 * Simulations: time complexity O(n_), space complexity O(n_)
 */
public static void rotate5(int[][] matrix) {
    int m = matrix.length;
    int n = matrix[0].length;
    if (m != n)
        return;
    int[][] result = new int[n][n];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            result[j][n - 1 - i] = matrix[i][j];
        }
    //print(result); / / print the result (method is no longer given)
}

Explanation: The advantage of this method is that it is easy to understand, the algorithm is simple, and the disadvantage is that it needs additional auxiliary space.

Algorithm improvement

If we do not apply for auxiliary space and operate directly on the original matrix, what should we do? If we assign values directly according to the above operations, the elements in the original array will be covered directly, and the data information will be lost. In another way, the result of 90 degrees clockwise inversion of the matrix is just that the array has lost its direction. In fact, the symmetric inversion of the matrix can also achieve the ultimate effect of this kind of drop. The difference is that 90 degrees clockwise inversion can not be achieved through a simple symmetric inversion operation, which may take many times. Now we need to find the transition between the symmetrical flip operation and the clockwise flip effect of 90 degrees.  
There are two kinds of symmetrical flip operations of_matrix, diagonal flip (main and secondary) and median flip (horizontal and vertical). Through the combination of these two flips, the clockwise flip of 90 degrees can be achieved. For example, when we analyze the simplest two-dimensional array {1,2}, {3,4}, we can get {1,3}, {2,4} after flipping once according to the principal diagonal, and {3,1}, {4,2} after flipping once according to the vertical midline, we can get {3,1}, {4,2}, which is the result we need. After permutation and combination screening, we can find four methods to get the final result.

  • Method 1: First turn it over according to the main diagonal line, then turn it over according to the vertical midline.

    /*
         * Inversion image 1: Principal diagonal inversion, vertical midline inversion time complexity O(n^2), spatial complexity O(1)
         */
    public static void rotate1(int[][] matrix) {
            int m = matrix.length;
            int n = matrix[0].length;
            if (m != n)
                return;
            int temp;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < i; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = temp;
                }
            System.out.println("After the principal diagonal reversal:");
            print(matrix);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n / 2; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[i][n - 1 - j];
                    matrix[i][n - 1 - j] = temp;
                }
            System.out.println("After the reversal of the vertical midline:");
            print(matrix);
        }
    
  • Method 2: First turn over according to the paradiagonal line, then turn over according to the horizontal midline.

    /*
         * Paradiagonal inversion, horizontal midline inversion time complexity O(n^2), spatial complexity O(1)
         */
    
        public static void rotate2(int[][] matrix) {
            int m = matrix.length;
            int n = matrix[0].length;
            if (m != n)
                return;
            int temp;
    
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n - 1 - i; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[n - 1 - j][n - 1 - i];
                    matrix[n - 1 - j][n - 1 - i] = temp;
                }
            System.out.println("After the sub-diagonal reversal:");
            print(matrix);
            for (int i = 0; i < n / 2; i++)
                for (int j = 0; j < n; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[n - 1 - i][j];
                    matrix[n - 1 - i][j] = temp;
                }
            System.out.println("After reversing the horizontal midline:");
            print(matrix);
        }
    
  • Method 3: First flip according to the horizontal midline, then flip according to the principal diagonal line.

    /*
         * Horizontal midline inversion, principal diagonal inversion time complexity O(n^2), spatial complexity O(1)
         */
        public static void rotate3(int[][] matrix) {
            int m = matrix.length;
            int n = matrix[0].length;
            if (m != n)
                return;
            int temp;
            for (int i = 0; i < n / 2; i++)
                for (int j = 0; j < n; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[n - 1 - i][j];
                    matrix[n - 1 - i][j] = temp;
                }
            System.out.println("After reversing the horizontal midline:");
            print(matrix);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < i; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = temp;
                }
            System.out.println("After the principal diagonal reversal:");
            print(matrix);
    
        }
    
  • Method 4: First flip according to the vertical midline and then vice-diagonal line.

    /*
         * Vertical midline inversion, sub-diagonal inversion time complexity O(n^2), space complexity O(1)
         */
        public static void rotate4(int[][] matrix) {
            int m = matrix.length;
            int n = matrix[0].length;
            if (m != n)
                return;
            int temp;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n / 2; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[i][n - 1 - j];
                    matrix[i][n - 1 - j] = temp;
                }
            System.out.println("After the reversal of the vertical midline:");
            print(matrix);
    
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n - 1 - i; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[n - 1 - j][n - 1 - i];
                    matrix[n - 1 - j][n - 1 - i] = temp;
                }
            System.out.println("After the sub-diagonal reversal:");
            print(matrix);
        } 
    

    Explanation: The above four methods are basically similar. The key is to find out the quantitative relationship between elements (i,j) and new positions (m,n) after symmetry reversal. In addition, the print(int[][] martrix) method mentioned in the code to print arrays is no longer given, and the reader can supplement it.

  • Demo Download

Posted by reto on Sat, 01 Jun 2019 15:27:33 -0700