Rotate the square matrix So easy

In the process of brushing the questions , some common operations of the matrix have been accumulated, and here are the characteristics to make some notes for subsequent advanced learning.

insert image description here

1. Basic operation


1.1 Flip the square matrix along the main diagonal

insert image description here
Code:

public void rotate(int[][] matrix) {
    
    
	// 1. 临界条件
	if(matrix == null || matrix.length == 0) return;
	// 注意:方阵的行==列
	int rows = matrix.length;
	// 2. 核心逻辑
	for(int i = 0; i < rows; i++) {
    
    
		for(int j = i ; j < rows; j++){
    
    
			int tmp = matrix[i][j];
			matrix[i][j] = matrix[j][i];
			matrix[j][i] = tmp;
		}
	}
}

1.2 Flip the square matrix along the sub-diagonal in situ

insert image description here
Code:

public void rotate(int[][] matrix) {
    
    
	// 1. 临界条件
	if(matrix == null || matrix.length == 0) return;
	// 注意:方阵的行==列
	int rows = matrix.length;
	// 2. 核心逻辑
	for(int i = 0; i < rows; i++) {
    
    
		// 和主对角线的差别在于这里。
		for(int j = 0 ; j < rows - i; j++){
    
    
			int tmp = matrix[i][j];
			matrix[i][j] = matrix[rows-1 - j][rows-1 - i];
			matrix[rows - 1 - j][rows - 1 - i] = tmp;
		}
	}
}

1.3 Flip the square matrix along the vertical line

insert image description here

public void rotate(int[][] matrix) {
    
    
	// 1. 临界条件
	if(matrix == null || matrix.length == 0) return;
	// 注意:方阵的行==列
	int rows = matrix.length;
	// 2. 核心逻辑:对每一层都进行翻转。
	for(int i = 0; i < rows; i++) {
    
    
		int left =0;
		int right = rows - 1;
		while(left < right) {
    
    
			int tmp = matrix[left][right];
			matrix[left][right] = matrix[right][left];
			matrix[right][left] = tmp;
		}
	}
}

2. Advanced

Students who often brush questions may notice that many of the so-called medium difficulty (Medium) and difficult difficulty (Hard) concentrate N (N>1) basic skills into one question. (PS: Don't complain about the difficulty factor of Leetcode here to show how "scientific" it is)

insert image description here
The picture above corresponds to 48. Rotate image - M

2.1 Idea 1

insert image description here
You can observe the three pictures above, and the ideas for solving the problem are all in it.
Implementation 1:

Flip diagonally first , then vertically ;

 public void rotate(int[][] matrix) {
    
    
        int rows = matrix.length;
      int n = matrix.length;
        //1. 先沿正对角线翻转
        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;
       }
		// 2. 沿竖直线翻转
        for(int i = 0; i < rows; i++){
    
    

            int left = 0;
            int right = rows-1;

            while(left < right){
    
    
                int tmp = matrix[i][left];
                matrix[i][left] = matrix[i][right];
                matrix[i][right] = tmp;

                left ++;
                right --;
            }
        }
    }

Implementation 2:

Flip vertically first , then flip diagonally ;

     public void rotate(int[][] matrix) {
    
    
        if(matrix == null || matrix.length == 0){
    
    
            return ;
        }

        int rows = matrix.length;

         // 1. 竖直线翻转
        for( int i = 0; i < rows; i++){
    
    
             int left = 0;
             int right = rows- 1;
            while(left < right){
    
    
                int tmp = matrix[i][left];
                matrix[i][left] = matrix[i][right];
                matrix[i][right] = tmp;            

                left++;
                right--;
             }
        }
		// 2. 副对角线翻转
        for(int i = 0 ; i < rows; i++){
    
    
            for(int j = 0; j < rows - i; j++){
    
    
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[rows-1-j][rows-1-i];
                matrix[rows-1-j][rows-1-i] = tmp;
            }
        }
       
    }     
  

2.2 Idea 2

insert image description here

Idea:
Each i represents a circle, regardless of whether len is odd or even, there are a total of len/2 circles to be rotated, rotating from the outer circle to the inside until the center point or the innermost circle

public void rotate(int[][] matrix) {
    
    
        int len = matrix.length;
        // i 代表圈次,一共要进行 len /2 的旋转。
        for (int i = 0; i < len / 2; i++) {
    
    
            int start = i;
            int end = len - i - 1;
            for (int j = 0; j < end - start; j++) {
    
    
                int temp = matrix[start][start + j];
                matrix[start][start + j] = matrix[end - j][start];
                matrix[end - j][start] = matrix[end][end - j];
                matrix[end][end - j] = matrix[start + j][end];
                matrix[start + j][end] = temp;
            }
        }      
    }

Guess you like

Origin blog.csdn.net/wangcheeng/article/details/122839614