-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral-matrix.js
57 lines (42 loc) · 1.13 KB
/
spiral-matrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if(!matrix.length || !matrix[0].length){
return [];
}
//Use 4 pointes to create wall around square
let rowBegin = 0,
rowEnd = matrix.length - 1,
colBegin = 0,
colEnd = matrix[0].length - 1;
let result = [];
while(rowBegin <= rowEnd && colBegin <= colEnd){
//move right
for(let i= colBegin; i<= colEnd; i++){
result.push(matrix[rowBegin][i]);
}
rowBegin++; // mark row as traversed after moving right
//move down
for(let i=rowBegin; i<= rowEnd; i++){
result.push(matrix[i][colEnd]);
}
colEnd--; //mark column as traversed after moving down
//move left
if(rowBegin <= rowEnd){
for(let i=colEnd; i >= colBegin; i--){
result.push(matrix[rowEnd][i]);
}
}
rowEnd--; //mark end row as traversed after moving left
//move up
if(colBegin <= colEnd){
for(let i=rowEnd; i >= rowBegin; i--){
result.push(matrix[i][colBegin]);
}
}
colBegin++; //mark begining column as traversed after moving up
}
return result;
};