-
Notifications
You must be signed in to change notification settings - Fork 0
/
51-spiral-matrix.js
46 lines (45 loc) · 1.02 KB
/
51-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
https://leetcode-cn.com/problems/spiral-matrix
/**
* time: O(n * m)
* space: O(n * m)
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
let result = [];
if (
matrix === null ||
matrix.length === 0 ||
matrix[0] === null ||
matrix[0].length === 0
) {
return result;
}
let rowBegin = 0;
let rowEnd = matrix.length - 1;
let colBegin = 0;
let colEnd = matrix[0].length - 1;
while (rowBegin <= rowEnd && colBegin <= colEnd) {
for (let i = colBegin; i <= colEnd; i++) {
result.push(matrix[rowBegin][i]);
}
rowBegin++;
for (let i = rowBegin; i <= rowEnd; i++) {
result.push(matrix[i][colEnd]);
}
colEnd--;
if (rowBegin <= rowEnd) {
for (let i = colEnd; i >= colBegin; i--) {
result.push(matrix[rowEnd][i]);
}
}
rowEnd--;
if (colBegin <= colEnd) {
for (let i = rowEnd; i >= rowBegin; i--) {
result.push(matrix[i][colBegin]);
}
}
colBegin++;
}
return result;
};