LeetCode_ Array_ Medium_ 59. Spiral matrix II

Posted by kapishi on Wed, 22 Dec 2021 06:40:25 +0100

1. Title

Give you a positive integer n to generate an n x n square matrix containing all elements from 1 to n2 and the elements are spirally arranged in clockwise order.

Example 1:

Input: n = 3
Output: [[1,2,3], [8,9,4], [7,6,5]]

Example 2:
Input: n = 1
Output: [[1]]

Tips:
1 <= n <= 20
Source: LeetCode
Link: https://leetcode-cn.com/problems/spiral-matrix-ii

2. Ideas

(1) The idea and LeetCode_54. Spiral matrix similar. According to the analysis of the topic, when generating integers in the matrix in a clockwise spiral order, start from the starting point matrix[0][0], and always start in the direction of right → down → left → up → right → down → until the whole matrix is filled. Therefore, it can be considered to generate all integers in the matrix according to this law. The specific steps are as follows:
(1.1) define a matrix with n rows and columns;
(1.2) in order to prevent the generated integers from being overwritten when generating integers in the above direction, you can set a visited array with the same row and column as the matrix, and its element type is boolean. If visited[i][j]=true, it indicates that the element matrix[i][j] in the matrix has been generated, otherwise it indicates that it has not been generated;
(1.3) in order to generate integers in the above direction in the program, the direction can be used to represent the direction in which integers are to be generated, and direction=1, 2, 3 and 4 respectively represent the generation of integers in the right, down, left and up directions in the matrix;
(1.4) set the starting point coordinates I and j. in this question, the starting point integer is matrix[0][0], so its initial value is num=1, and then set visited[i][j]=true, that is, the starting point has generated an integer. After the above preparations are completed, you can start to generate an integer circularly;
(1.5) in the process of generating integers, use the if else statement to judge the direction of traversal, and save the generated integers at a position in the meta matrix every time you access them. In addition, in the process of generating integers, pay special attention to prevent the array from crossing the boundary and integer coverage. When the above four directions of a position cannot generate integers, the It indicates that all positions of the matrix have been filled. At this time, just exit the loop and return to the matrix.

3. Code implementation (Java)

//(1) Train of thought 1
public int[][] generateMatrix(int n) {
    int[][] matrix = new int[n][n];
    //visited[i][j]==true: the element matrix[i][j] in the matrix has been generated
    boolean[][] visited = new boolean[n][n];
    //direction=1, 2, 3 and 4 respectively indicate that integers are generated to the right, bottom, left and top of the matrix
    //Initially, follow the steps of right → down → left → up → right → down → Start generating integers in the direction of until the entire matrix is filled
    int direction=1;
    //Defines the abscissa and ordinate of the starting point of the generated integer
    int i=0,j=0;
    //Let the generated starting integer num=1
    int num = 1;
    matrix[i][j]=num;
    //Set the starting point of the generated integer to the accessed state
    visited[i][j]=true;
    while(true){
        if(direction==1 && j+1<n && visited[i][j+1]==false){
            //Generate integer to the right
            while(j+1<n && visited[i][j+1]==false){
                matrix[i][j+1]=++num;
                visited[i][j+1]=true;
                j++;
            }
            direction++;        
        }else if(direction==2 && i+1<n && visited[i+1][j]==false){
            //Generate integer down
            while(i+1<n && visited[i+1][j]==false){
                matrix[i+1][j]=++num;
                visited[i+1][j]=true;
                i++;
            }
            direction++;
        }else if(direction==3 && j-1>=0 && visited[i][j-1]==false){
            //Generate integer to the left
            while(j-1>=0 && visited[i][j-1]==false){
                matrix[i][j-1]=++num;
                visited[i][j-1]=true;
                j--;;
            }
            direction++;
        }else if(direction==4 && i-1>=0 && visited[i-1][j]==false){
            //Generate integer up
            while(i-1>=0 && visited[i-1][j]==false){
                matrix[i-1][j]=++num;
                visited[i-1][j]=true;
                i--;
            }
            direction=1;
        }else{
            //End of generation integer
            break;
        }
    }
    return matrix;
}

Topics: Algorithm leetcode array