-
Notifications
You must be signed in to change notification settings - Fork 38
/
spiral-matrix.js
44 lines (43 loc) · 1.03 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
var spiralOrder = function(matrix) {
let result = [];
let flag = [];
for (let i = 0; i < matrix.length; i++) {
flag[i] = [];
for (let j = 0; j < matrix[i].length; j++) {
flag[i][j] = 0;
}
}
let dr = [0, 1, 0, -1];
let dc = [1, 0, -1, 0];
let row = matrix.length,
column = matrix[0].length;
let r = 0,
c = 0,
i = 0,
k = 0;
for (i = 0; i < row * column; i++) {
flag[r][c] = 1;
result.push(matrix[r][c]);
let newR = r + dr[k];
let newC = c + dc[k];
if (
newR >= 0 &&
newR < row &&
newC >= 0 &&
newC < column &&
!flag[newR][newC]
) {
r = newR;
c = newC;
} else {
k++;
k = k % 4;
r += dr[k];
c += dc[k];
}
}
return result;
};
let ary = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
spiralOrder(ary);
//旋转矩阵 https://leetcode-cn.com/problems/spiral-matrix/