forked from dnp1204/daily-code-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem-65.js
68 lines (62 loc) · 1.18 KB
/
problem-65.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
58
59
60
61
62
63
64
65
66
67
68
/**
* Company: Amazon.
*
* Given a N by M matrix of numbers, print out the matrix in a clockwise spiral.
* For example, given the following matrix:
* [[1, 2, 3, 4, 5],
* [6, 7, 8, 9, 10],
* [11, 12, 13, 14, 15],
* [16, 17, 18, 19, 20]]
* You should print out the following:
* 1
* 2
* 3
* 4
* 5
* 10
* 15
* 20
* 19
* 18
* 17
* 16
* 11
* 6
* 7
* 8
* 9
* 14
* 13
* 12
*/
var spiralOrder = function(matrix) {
if (matrix.length === 0) return [];
const 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 j = colBegin; j <= colEnd; j++) {
result.push(matrix[rowBegin][j]);
}
rowBegin++;
for (let i = rowBegin; i <= rowEnd; i++) {
result.push(matrix[i][colEnd]);
}
colEnd--;
if (rowBegin <= rowEnd) {
for (let j = colEnd; j >= colBegin; j--) {
result.push(matrix[rowEnd][j]);
}
}
rowEnd--;
if (colBegin <= colEnd) {
for (let i = rowEnd; i >= rowBegin; i--) {
result.push(matrix[i][colBegin]);
}
}
colBegin++;
}
return result;
};