54. Spiral Matrix
Leetcode
題目
解答
var spiralOrder = function(matrix) {
var rotate = function(matrix) {
const m = matrix.length;
const n = matrix[0].length;
const result = Array.from({length: n}, () => []);
for(let i=0; i<m; i++) {
for(let j=0; j<n; j++) {
result[j].push(matrix[i][n-j-1]);
}
}
return result
};
const result = [];
while(matrix.length) {
const next = matrix.splice(0, 1)[0];
result.push(...next);
if (matrix.length) {
matrix = rotate(matrix);
}
}
return result;
};測資
參考資料
Last updated