-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54.螺旋矩阵.js
51 lines (50 loc) · 1019 Bytes
/
54.螺旋矩阵.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
/*
* @lc app=leetcode.cn id=54 lang=javascript
*
* [54] 螺旋矩阵
*/
// @lc code=start
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
let res = []
let l = 0, t = 0
let b = matrix.length
let r = matrix[0] && matrix[0].length
let x = 0, y = 0
while (l < r && t < b) {
while (x < r && y < b && x >= l && y >= t) {
res.push(matrix[y][x++])
}
x--
y++
t++
while (x < r && y < b && x >= l && y >= t) {
res.push(matrix[y++][x])
}
y--
x--
r--
while (x < r && y < b && x >= l && y >= t) {
res.push(matrix[y][x--])
}
x++
y--
b--
while (x < r && y < b && x >= l && y >= t) {
res.push(matrix[y--][x])
}
y++
x++
l++
}
return res
};
// @lc code=end
console.log(spiralOrder([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]))