给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

function spiralOrder(matrix: number[][]): number[] {
    let top =0
    let bottom = matrix.length-1
    let right = matrix[0].length-1
    let left = 0
 
    const result:number[]= []
 
    while(true){
        // 1. 从左到右 (处理 top 行)
        for (let i = left; i <= right; i++) result.push(matrix[top][i]);
        if (++top > bottom) break; // 处理完一行,上边界下移,若越界则结束
 
        // 2. 从上到下 (处理 right 列)
        for (let i = top; i <= bottom; i++) result.push(matrix[i][right]);
        if (--right < left) break; // 处理完一列,右边界左移
 
        // 3. 从右到左 (处理 bottom 行)
        for (let i = right; i >= left; i--) result.push(matrix[bottom][i]);
        if (--bottom < top) break; // 处理完一行,下边界上移
 
        // 4. 从下到上 (处理 left 列)
        for (let i = bottom; i >= top; i--) result.push(matrix[i][left]);
        if (++left > right) break; // 处理完一列,左边界右移
    }
 
    return result
};