Skip to content

Commit df8be91

Browse files
committed
Solutions With JS
1 parent 04ba67f commit df8be91

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Medium/59. Spiral Matrix II.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var generateMatrix = function(n) {
2+
const M = [...Array(n)].map(() => Array(n).fill(0));
3+
let x = 0, y = 0, dx = 1, dy = 0;
4+
5+
for(let i=1, nn = n**2; i<=nn; i++) {
6+
M[y][x] = i;
7+
if (!M[y + dy] || M[y + dy][x + dx] !== 0)
8+
[dx, dy] = [-dy, dx];
9+
x += dx;
10+
y += dy
11+
}
12+
return M;
13+
};

0 commit comments

Comments
 (0)