We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 数组, 矩阵, 模拟
给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
n
1
n x n
matrix
示例 1:
输入:n = 3 输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入:n = 1 输出:[[1]]
提示:
1 <= n <= 20
Language: JavaScript
/** * @param {number} n * @return {number[][]} */ var generateMatrix = function(n) { let left = 0 let right = n - 1 let bottom = n - 1 let top = 0 const total = n * n const dep = [] for (let i = 0; i < n; i++) { dep[i] = [] } let count = 0 while (count < total) { for (let i = left; i <= right; i++) dep[left][i] = ++count top++ for (let i = top; i <= bottom; i++) dep[i][right] = ++count right-- for (let i = right; i >= left; i--) dep[bottom][i] = ++count bottom-- for (let i = bottom; i >= top; i--) dep[i][left] = ++count left++ } return dep };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
59. 螺旋矩阵 II
Description
Difficulty: 中等
Related Topics: 数组, 矩阵, 模拟
给你一个正整数
n
,生成一个包含1
到 n2 所有元素,且元素按顺时针顺序螺旋排列的n x n
正方形矩阵matrix
。示例 1:
示例 2:
提示:
1 <= n <= 20
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: