classSolution { public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> matrix(n, vector<int>(n, 0)); int top = 0, bottom = n - 1, left = 0, right = n - 1; int num = 1; while (num <= n * n) { for (int i = left; i <= right; ++i) { matrix[top][i] = num++; } ++top; for (int i = top; i <= bottom; ++i) { matrix[i][right] = num++; } --right; for (int i = right; i >= left; --i) { matrix[bottom][i] = num++; } --bottom; for (int i = bottom; i >= top; --i) { matrix[i][left] = num++; } ++left; } return matrix; } };
classSolution(object): defgenerateMatrix(self, n): """ :type n: int :rtype: List[List[int]] """ matrix = [[0] * n for _ inrange(n)] top, bottom, left, right = 0, n - 1, 0, n - 1 num = 1 while num <= n * n: for i inrange(left, right + 1): matrix[top][i] = num num += 1 top += 1 for i inrange(top, bottom + 1): matrix[i][right] = num num += 1 right -= 1 for i inrange(right, left - 1, -1): matrix[bottom][i] = num num += 1 bottom -= 1 for i inrange(bottom, top - 1, -1): matrix[i][left] = num num += 1 left += 1 return matrix
classSolution: defgenerateMatrix(self, n: int) -> List[List[int]]: matrix = [[0] * n for _ inrange(n)] top, bottom, left, right = 0, n - 1, 0, n - 1 num = 1 while num <= n * n: for i inrange(left, right + 1): matrix[top][i] = num num += 1 top += 1 for i inrange(top, bottom + 1): matrix[i][right] = num num += 1 right -= 1 for i inrange(right, left - 1, -1): matrix[bottom][i] = num num += 1 bottom -= 1 for i inrange(bottom, top - 1, -1): matrix[i][left] = num num += 1 left += 1 return matrix
/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */ int** generateMatrix(int n, int* returnSize, int** returnColumnSizes) { int** matrix = (int**)malloc(n * sizeof(int*)); for (int i = 0; i < n; ++i) { matrix[i] = (int*)malloc(n * sizeof(int)); } int top = 0, bottom = n - 1, left = 0, right = n - 1; int num = 1; while (num <= n * n) { for (int i = left; i <= right; ++i) { matrix[top][i] = num++; } ++top; for (int i = top; i <= bottom; ++i) { matrix[i][right] = num++; } --right; for (int i = right; i >= left; --i) { matrix[bottom][i] = num++; } --bottom; for (int i = bottom; i >= top; --i) { matrix[i][left] = num++; } ++left; } *returnSize = n; *returnColumnSizes = (int*)malloc(n * sizeof(int)); for (int i = 0; i < n; ++i) { (*returnColumnSizes)[i] = n; } return matrix; }
publicclassSolution { publicint[][] GenerateMatrix(int n) { int[][] matrix = newint[n][]; for (int i = 0; i < n; i++) { matrix[i] = newint[n]; } int top = 0, bottom = n - 1, left = 0, right = n - 1; int num = 1; while (num <= n * n) { for (int i = left; i <= right; i++) { matrix[top][i] = num++; } top++; for (int i = top; i <= bottom; i++) { matrix[i][right] = num++; } right--; for (int i = right; i >= left; i--) { matrix[bottom][i] = num++; } bottom--; for (int i = bottom; i >= top; i--) { matrix[i][left] = num++; } left++; } return matrix; } }
/** * @param {number} n * @return {number[][]} */ var generateMatrix = function(n) { let matrix = newArray(n).fill().map(() =>newArray(n).fill(0)); let top = 0, bottom = n - 1, left = 0, right = n - 1; let num = 1; while (num <= n * n) { for (let i = left; i <= right; i++) { matrix[top][i] = num++; } top++; for (let i = top; i <= bottom; i++) { matrix[i][right] = num++; } right--; for (let i = right; i >= left; i--) { matrix[bottom][i] = num++; } bottom--; for (let i = bottom; i >= top; i--) { matrix[i][left] = num++; } left++; } return matrix; };
functiongenerateMatrix(n: number): number[][] { constmatrix: number[][] = Array.from({ length: n }, () =>Array(n).fill(0)); let top = 0, bottom = n - 1, left = 0, right = n - 1; let num = 1; while (num <= n * n) { for (let i = left; i <= right; i++) { matrix[top][i] = num++; } top++; for (let i = top; i <= bottom; i++) { matrix[i][right] = num++; } right--; for (let i = right; i >= left; i--) { matrix[bottom][i] = num++; } bottom--; for (let i = bottom; i >= top; i--) { matrix[i][left] = num++; } left++; } return matrix; }
classSolution { fungenerateMatrix(n: Int): Array<IntArray> { val matrix = Array(n) { IntArray(n) } var left = 0 var right = n - 1 var top = 0 var bottom = n - 1 var num = 1 while (left <= right && top <= bottom) { for (i in left..right) { matrix[top][i] = num++ } top++ for (i in top..bottom) { matrix[i][right] = num++ } right-- if (top <= bottom) { for (i in right downTo left) { matrix[bottom][i] = num++ } bottom-- } if (left <= right) { for (i in bottom downTo top) { matrix[i][left] = num++ } left++ } } return matrix } }
funcgenerateMatrix(n int) [][]int { matrix := make([][]int, n) for i := range matrix { matrix[i] = make([]int, n) } left, right, top, bottom := 0, n-1, 0, n-1 num := 1 for left <= right && top <= bottom { for i := left; i <= right; i++ { matrix[top][i] = num num++ } top++ for i := top; i <= bottom; i++ { matrix[i][right] = num num++ } right-- if top <= bottom { for i := right; i >= left; i-- { matrix[bottom][i] = num num++ } bottom-- } if left <= right { for i := bottom; i >= top; i-- { matrix[i][left] = num num++ } left++ } } return matrix }
# @param {Integer} n # @return {Integer[][]} defgenerate_matrix(n) matrix = Array.new(n) { Array.new(n, 0) } left, right, top, bottom = 0, n - 1, 0, n - 1 num = 1 while left <= right && top <= bottom left.upto(right) do |i| matrix[top][i] = num num += 1 end top += 1 top.upto(bottom) do |i| matrix[i][right] = num num += 1 end right -= 1 if top <= bottom right.downto(left) do |i| matrix[bottom][i] = num num += 1 end bottom -= 1 end if left <= right bottom.downto(top) do |i| matrix[i][left] = num num += 1 end left += 1 end end matrix end