lc螺旋矩阵

lc螺旋矩阵

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var spiralOrder = function (matrix) {
    if (matrix.length === 0) return []
    const res = []
    let top = 0, left = 0, bottom = matrix.length - 1, right = matrix[0].length - 1
    while (top < bottom && left < right) {
        for (let i = left; i < right; i++) res.push(matrix[top][i])   // 上层
        for (let i = top; i < bottom; i++) res.push(matrix[i][right]) // 右层
        for (let i = right; i > left; i--) res.push(matrix[bottom][i])// 下层
        for (let i = bottom; i > top; i--) res.push(matrix[i][left])  // 左层
        right--
         bottom--
        top++
        left++  // 四个边界同时收缩,进入内层
    }
    if (top === bottom) // 剩下一行,从左到右依次添加
        for (let i = left; i <= right; i++) res.push(matrix[top][i])
    else if (left === right) // 剩下一列,从上到下依次添加
        for (let i = top; i <= bottom; i++) res.push(matrix[i][left])
    return res
};
上一篇:【CF446D】DZY Loves Games


下一篇:【线代&NumPy】第三章 - 行列课后练习 | 简述并提供代码