题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1bc7076fe456fca16b95f57f09243d91.png)
代码
var spiralOrder = function(matrix) {
const m = matrix.length
const n = matrix[0].length
const res = []
let upper_bound = 0, right_bound = n-1;
let lower_bound = m-1, left_bound = 0;
while(res.length < m*n) {
if(upper_bound <= lower_bound) {
for (let i = left_bound; i <= right_bound; i++) {
res.push(matrix[upper_bound][i])
}
upper_bound++
}
if(right_bound >= left_bound) {
for (let i = upper_bound; i <= lower_bound; i++) {
res.push(matrix[i][right_bound])
}
right_bound--
}
if(lower_bound >= upper_bound) {
for (let i = right_bound; i >= left_bound; i--) {
res.push(matrix[lower_bound][i])
}
lower_bound--
}
if(left_bound <= right_bound) {
for (let i = lower_bound; i >= upper_bound; i--) {
res.push(matrix[i][left_bound])
}
left_bound++
}
}
return res
};
参考资料