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

Appearance settings

Commit cb8c906

Browse filesBrowse files
committed
54th problem
1 parent 5355058 commit cb8c906
Copy full SHA for cb8c906

File tree

1 file changed

+31
-0
lines changed
Filter options

1 file changed

+31
-0
lines changed

‎54. Spiral Matrix.js

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var spiralOrder = function(matrix) {
2+
if(matrix.length == 0) { return a}
3+
let [a,rB,rE,cB,cE] = [[],0,matrix.length-1,0,matrix[0].length - 1]
4+
5+
while(rB <= rE && cB <= cE) {
6+
for(let j = cB; j <= cE; j ++){
7+
a.push(matrix[rB][j])
8+
}
9+
rB++;
10+
11+
for(let j = rB; j <= rE; j ++){
12+
a.push(matrix[j][cE])
13+
}
14+
cE--;
15+
16+
if(rB <= rE){
17+
for(let j = cE; j >= cB; j --){
18+
a.push(matrix[rE][j])
19+
}
20+
}
21+
rE--;
22+
23+
if(cB <= cE){
24+
for(let j = rE; j >= rB; j --){
25+
a.push(matrix[j][cB])
26+
}
27+
}
28+
cB ++
29+
}
30+
return a
31+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.