59. 螺旋矩阵 II
给你一个正整数 n
,生成一个包含 1
到 n2
所有元素,且元素按顺时针顺序螺旋排列的 n x n
正方形矩阵 matrix
。
示例 1:
输入:n = 3 输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入:n = 1 输出:[[1]]
提示:
1 <= n <= 20
相似题目
原站题解
python3 解法, 执行用时: 46 ms, 内存消耗: 16.4 MB, 提交时间: 2024-04-20 16:23:47
class Solution: def generateMatrix(self, n: int) -> List[List[int]]: matrix = [[0] * n for _ in range(n)] num = 1 left, right, top, bottom = 0, n - 1, 0, n - 1 while left <= right and top <= bottom: for col in range(left, right + 1): matrix[top][col] = num num += 1 for row in range(top + 1, bottom + 1): matrix[row][right] = num num += 1 if left < right and top < bottom: for col in range(right - 1, left, -1): matrix[bottom][col] = num num += 1 for row in range(bottom, top, -1): matrix[row][left] = num num += 1 left += 1 right -= 1 top += 1 bottom -= 1 return matrix
javascript 解法, 执行用时: 56 ms, 内存消耗: 49.7 MB, 提交时间: 2024-04-20 16:23:20
/** * @param {number} n * @return {number[][]} */ var generateMatrix = function(n) { const maxNum = n * n; let curNum = 1; const matrix = new Array(n).fill(0).map(() => new Array(n).fill(0)); let row = 0, column = 0; const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; // 右下左上 let directionIndex = 0; while (curNum <= maxNum) { matrix[row][column] = curNum; curNum++; const 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; };
javascript 解法, 执行用时: 55 ms, 内存消耗: 49.2 MB, 提交时间: 2024-04-20 16:22:37
/** * @param {number} n * @return {number[][]} */ var generateMatrix = function(n) { let num = 1; const matrix = new Array(n).fill(0).map(() => new Array(n).fill(0)); let left = 0, right = n - 1, top = 0, bottom = n - 1; while (left <= right && top <= bottom) { for (let column = left; column <= right; column++) { matrix[top][column] = num; num++; } for (let row = top + 1; row <= bottom; row++) { matrix[row][right] = num; num++; } if (left < right && top < bottom) { for (let column = right - 1; column > left; column--) { matrix[bottom][column] = num; num++; } for (let row = bottom; row > top; row--) { matrix[row][left] = num; num++; } } left++; right--; top++; bottom--; } return matrix; };
golang 解法, 执行用时: 0 ms, 内存消耗: 2.2 MB, 提交时间: 2024-04-20 16:22:21
func generateMatrix(n int) [][]int { matrix := make([][]int, n) for i := range matrix { matrix[i] = make([]int, n) } num := 1 left, right, top, bottom := 0, n-1, 0, n-1 for left <= right && top <= bottom { for column := left; column <= right; column++ { matrix[top][column] = num num++ } for row := top + 1; row <= bottom; row++ { matrix[row][right] = num num++ } if left < right && top < bottom { for column := right - 1; column > left; column-- { matrix[bottom][column] = num num++ } for row := bottom; row > top; row-- { matrix[row][left] = num num++ } } left++ right-- top++ bottom-- } return matrix }
golang 解法, 执行用时: 0 ms, 内存消耗: 2.2 MB, 提交时间: 2024-04-20 16:22:09
type pair struct{ x, y int } var dirs = []pair{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} // 右下左上 func generateMatrix(n int) [][]int { matrix := make([][]int, n) for i := range matrix { matrix[i] = make([]int, n) } row, col, dirIdx := 0, 0, 0 for i := 1; i <= n*n; i++ { matrix[row][col] = i dir := dirs[dirIdx] if r, c := row+dir.x, col+dir.y; r < 0 || r >= n || c < 0 || c >= n || matrix[r][c] > 0 { dirIdx = (dirIdx + 1) % 4 // 顺时针旋转至下一个方向 dir = dirs[dirIdx] } row += dir.x col += dir.y } return matrix }
cpp 解法, 执行用时: 0 ms, 内存消耗: 7.6 MB, 提交时间: 2024-04-20 16:21:43
class Solution { public: vector<vector<int>> generateMatrix(int n) { int num = 1; vector<vector<int>> matrix(n, vector<int>(n)); int left = 0, right = n - 1, top = 0, bottom = n - 1; while (left <= right && top <= bottom) { for (int column = left; column <= right; column++) { matrix[top][column] = num; num++; } for (int row = top + 1; row <= bottom; row++) { matrix[row][right] = num; num++; } if (left < right && top < bottom) { for (int column = right - 1; column > left; column--) { matrix[bottom][column] = num; num++; } for (int row = bottom; row > top; row--) { matrix[row][left] = num; num++; } } left++; right--; top++; bottom--; } return matrix; } };
java 解法, 执行用时: 0 ms, 内存消耗: 40.6 MB, 提交时间: 2024-04-20 16:21:29
class Solution { public int[][] generateMatrix(int n) { int num = 1; int[][] matrix = new int[n][n]; int left = 0, right = n - 1, top = 0, bottom = n - 1; while (left <= right && top <= bottom) { for (int column = left; column <= right; column++) { matrix[top][column] = num; num++; } for (int row = top + 1; row <= bottom; row++) { matrix[row][right] = num; num++; } if (left < right && top < bottom) { for (int column = right - 1; column > left; column--) { matrix[bottom][column] = num; num++; } for (int row = bottom; row > top; row--) { matrix[row][left] = num; num++; } } left++; right--; top++; bottom--; } return matrix; } }
java 解法, 执行用时: 0 ms, 内存消耗: 40.3 MB, 提交时间: 2024-04-20 16:21:16
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; } }
python3 解法, 执行用时: 40 ms, 内存消耗: 15 MB, 提交时间: 2022-06-21 11:53:59
class Solution: def generateMatrix(self, n: int) -> List[List[int]]: dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] matrix = [[0] * n for _ in range(n)] row, col, dirIdx = 0, 0, 0 for i in range(n * n): matrix[row][col] = i + 1 dx, dy = dirs[dirIdx] r, c = row + dx, col + dy if r < 0 or r >= n or c < 0 or c >= n or matrix[r][c] > 0: dirIdx = (dirIdx + 1) % 4 # 顺时针旋转至下一个方向 dx, dy = dirs[dirIdx] row, col = row + dx, col + dy return matrix