function spiralOrder(arr) {
const result = [];
if (!arr || arr.length === 0) {
return result;
}
let m = arr.length - 1;
let n = arr[0].length - 1;
let l = 0; // 行
let u = 0; //竖
while(true) {
// 行
for (let i = l; i <= n; i++) {
result.push(arr[l][i]);
}
++u;
if (u>m) {
break;
}
for (let i = u; i <= m; i++) {
result.push(arr[i][n]);
}
--n;
if (n < l) {
break;
}
for (let i = n; i >= l; i--) {
result.push(arr[m][i]);
}
--m;
if (u > m) {
break;
}
for (let i = m; i >= u; i--) {
result.push(arr[i][l]);
}
l++
if (n < l) {
break;
}
}
return result;
}
console.log(spiralOrder([[1,2,3], [4,5,6], [7,8,9]]));
console