给你一个正整数 n
,生成一个包含 1
到 n2
所有元素,且元素按顺时针顺序螺旋排列的 n x n
正方形矩阵 matrix
。
示例 1:
输入:n = 3 输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入:n = 1 输出:[[1]]
方法一:边界控制
class Solution { public int[][] generateMatrix(int n) { int[][] result = new int[n][n]; if (n < 1)return null; int i = 0,j = 0; int left = 0, right = n-1,top = 0, down = n-1; int num = 1; while (num != n*n + 1) { for (i = left; i <= right; i++) { result[top][i] = num; num++; } top++; for (j = top; j <= down; j ++){ result[j][right] = num; num++; } right--; for (i = right; i >= left; i --){ result[down][i] = num; num++; } down--; for (j = down; j >= top;j --){ result[j][left] = num; num++; } left++; } return result; } }
方法二:官方题解
class Solution { public int[][] generateMatrix(int n) { int maxNum = n * n; int curNum = 1; int[][] matrix = new int[n][n]; int row = 0, column = 0; int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 右下左上 int directionIndex = 0; while (curNum <= maxNum) { matrix[row][column] = curNum; curNum++; int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1]; if (nextRow < 0 || nextRow >= n || nextColumn < 0 || nextColumn >= n || matrix[nextRow][nextColumn] != 0) { directionIndex = (directionIndex + 1) % 4; // 顺时针旋转至下一个方向 } row = row + directions[directionIndex][0]; column = column + directions[directionIndex][1]; } return matrix; } }
标签:directionIndex,21,--,II,int,num,down,left From: https://www.cnblogs.com/xinger123/p/16902624.html