Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite1f91d7

Browse files
authored
Merge pull requestneetcode-gh#2087 from nikkikharkwal/main
Create: 0059-spiral-matrix-ii.js
2 parents1c0b59b +09a2991 commite1f91d7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎javascript/0059-spiral-matrix-ii.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* https://leetcode.com/problems/spiral-matrix-ii/
3+
*@param {number} n
4+
*@return {number[][]}
5+
*/
6+
vargenerateMatrix=function(n){
7+
letmatrix=Array.from(Array(n),()=>Array(n));
8+
9+
lettop=0,bottom=n-1,left=0,right=n-1;
10+
letelement=1;
11+
12+
while(top<=bottom&&left<=right){
13+
14+
for(leti=left;i<=right;i++){
15+
matrix[top][i]=element++;
16+
}
17+
top++;
18+
19+
for(leti=top;i<=bottom;i++){
20+
matrix[i][right]=element++;
21+
}
22+
right--;
23+
24+
for(leti=right;i>=left;i--){
25+
matrix[bottom][i]=element++;
26+
}
27+
bottom--;
28+
29+
for(leti=bottom;i>=top;i--){
30+
matrix[i][left]=element++;
31+
}
32+
left++;
33+
}
34+
returnmatrix;
35+
};
36+
37+
// Runtime: 54 ms, 0.62% of solutions used 54 ms of runtime for spiral matrix ii.
38+
// Memory Usage: 41.9 MB, 7.07% of solutions used 41.9 MB of memory for spiral matrix ii.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp