https://leetcode-cn.com/problems/spiral-matrix/
数组
基础

function spiralOrder(matrix: number[][]): number[] { const result: number[] = []; let top = 0 let bottom = matrix.length - 1 let left = 0 let right = matrix[0] ? matrix[0].length - 1 : 0 while(top <= bottom && left <= right) { // 1 * N only → for(let i = left; i <= right && top === bottom; i++) { result.push(matrix[top][i]) } // N * 1 (N > 1) only ↓ for(let i = top; i <= bottom && right === top && top !== bottom; i++) { result.push(matrix[i][right]) } // → for(let i = left; i < right && bottom > top; i++) { result.push(matrix[top][i]) } // ↓ for(let i = top; i < bottom && right > top; i++) { result.push(matrix[i][right]) } // ← for(let i = right; i > left && bottom > top; i--) { result.push(matrix[bottom][i]) } // ↑ for(let i = bottom; i > top && right > top; i--) { result.push(matrix[i][left]) } top++ bottom-- left++ right-- } return result};