Given a positive integer n, generate an n x n matrix filled with elements from 1 to n^2 in spiral order.

Example 1:

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

Example 2:

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

Constraints:

impl Solution {
    pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
        // 행렬 초기화
        **let n = n as usize;**
        let mut matrix: **Vec<Vec<i32>>** = **vec![vec![0; n]; n];**
        let mut num = 1;
        let mut start_row = 0;
        let mut end_row = n - 1;
        let mut start_col = 0;
        let mut end_col = n - 1;

        while start_row <= end_row && start_col <= end_col {
            // 위쪽 행 탐색 (왼쪽에서 오른쪽)
            for col in **start_col..=end_col** {
                matrix[start_row][col] = num;
                num += 1;
            }
            start_row += 1;

            // 오른쪽 열 탐색 (위에서 아래로)
            for row in **start_row..=end_row** {
                matrix[row][end_col] = num;
                num += 1;
            }
            end_col -= 1;

            // 아래쪽 행 탐색 (오른쪽에서 왼쪽)
            if start_row <= end_row {
                for col in **(start_col..=end_col).rev()** {
                    matrix[end_row][col] = num;
                    num += 1;
                }
                end_row -= 1;
            }

            // 왼쪽 열 탐색 (아래에서 위로)
            if start_col <= end_col {
                for row in **(start_row..=end_row).rev()** {
                    matrix[row][start_col] = num;
                    num += 1;
                }
                start_col += 1;
            }
        }

        matrix
    }
}