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

Commit19fe535

Browse files
committed
only add 54
1 parentb8dfff6 commit19fe535

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

‎Medium/54-spiralMatrix.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Key: Traverse from left to right, increase rowStart, means this top row has been visited.
3+
* Traverse from up to down, decrease colEnd, means this right column has been visited.
4+
* Traverse from right to left, decrease rowEnd, means this bottom row has been visited.
5+
* Traverse from down to up, increase colStart, means this left column has been visited.
6+
* when the rowStart === rowEnd, there is only this row left to visit, (left -> right)
7+
* when the colStart === colEnd, there is only this column left to visit, (up -> down)
8+
*
9+
*@param {number[][]} matrix
10+
*@return {number[]}
11+
*/
12+
varspiralOrder=function(matrix){
13+
if(matrix.length===0)returnmatrix;
14+
varrowStart=0;
15+
varcolStart=0;
16+
varrowEnd=matrix.length-1;
17+
varcolEnd=matrix[rowStart].length-1;
18+
varpath=[];
19+
20+
while(rowStart<=rowEnd&&colStart<=colEnd){
21+
if(rowStart===rowEnd){
22+
for(vari=colStart;i<=colEnd;i++){
23+
path.push(matrix[rowStart][i]);
24+
}
25+
break;
26+
}
27+
28+
if(colStart===colEnd){
29+
for(vari=rowStart;i<=rowEnd;i++){
30+
path.push(matrix[i][colEnd]);
31+
}
32+
break;
33+
}
34+
35+
for(vari=colStart;i<=colEnd;i++){
36+
path.push(matrix[rowStart][i]);
37+
}
38+
rowStart++;
39+
40+
for(vari=rowStart;i<=rowEnd;i++){
41+
path.push(matrix[i][colEnd]);
42+
}
43+
colEnd--;
44+
45+
for(vari=colEnd;i>=colStart;i--){
46+
path.push(matrix[rowEnd][i]);
47+
}
48+
rowEnd--;
49+
50+
for(vari=rowEnd;i>=rowStart;i--){
51+
path.push(matrix[i][colStart]);
52+
}
53+
colStart++;
54+
}
55+
56+
returnpath;
57+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp