xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

leetcode 螺旋矩阵算法题解 All In One

leetcode 螺旋矩阵算法题解 All In One

js / ts 实现螺旋矩阵

LeetCode 54. Spiral Matrix


"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-06-08
 * @modified
 *
 * @description 54. 螺旋矩阵
 * @description 54. Spiral Matrix
 * @difficulty Medium
 * @complexity O(n)
 * @time O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/spiral-matrix/
 * @link https://leetcode.cn/problems/spiral-matrix/
 * @solutions
 *
 * @best_solutions
 *
 */

// export {};

const log = console.log;

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */

function spiralOrder(matrix: number[][]): number[] {
  const result: number[] = [];
  // 行
  const m = matrix.length;
  // 列
  const n = matrix[0].length;
  // 仅有 1行或 1列,直接展平 matrix 即可
  if(m === 1 || n === 1) {
    // return matrix.reduce((acc, arr) => acc.concat(arr), []);
    return matrix.flat();
  }
  // solution 2: 遍历; 模拟,visited 标记矩阵 (麻烦,复杂度较高) \U0001f44e
  // solution 1: 遍历:按层次处理,一层一层的剥洋葱 left, right, top, bottom 四个顶点 \U0001f680
  // 左右,看列
  let left = 0;
  let right = n - 1;
  // 上下,看行
  let top = 0;
  let bottom = m - 1;
  // 按层遍历
  while(left <= right && top <= bottom) {
    // 最后剩的一层一定仅包含 Top 或 Top + Right
    // 上边
    for(let i = left; i <= right; i++) {
      // (top, left) => (top, right)
      result.push(matrix[top][i]);
    }
    // 右边
    for(let i = top + 1; i <= bottom; i++) {
      // (top + 1, right) => (bottom, right)
      result.push(matrix[i][right]);
    }
    // 关键条件 ✅ (是否存在 matrix 下边或左边, 排除 left === right 或 top === bottom 导致的循环)
    // left < right && top < bottom
    if(left < right && top < bottom) {
      // 下边
      for(let i = right - 1; i >= left + 1; i--) {
        // (bottom, right - 1) => (bottom, left + 1)
        result.push(matrix[bottom][i]);
      }
      // 左边
      for(let i = bottom; i >= top + 1; i--) {
        // (bottom, left) => (top + 1, left)
        result.push(matrix[i][left]);
      }
    }
    // 更新迭代条件
    left += 1;
    right -= 1;
    top += 1;
    bottom -= 1;
  }
  return result;
};

/*

// test cases

[[1,2,3],[4,5,6],[7,8,9]]
[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
[[1,2,3,4],[5,6,7,8],[9,10,11,12]]

[[2,5,8],[4,0,-1]]
[[1,2,3,4,5,6,7,8,9,10],[11,12,13,14,15,16,17,18,19,20]]
[[23,18,20,26,25],[24,22,3,4,4],[15,22,2,24,29],[18,15,23,28,28]]


https://leetcode.com/problems/spiral-matrix/

https://leetcode.cn/problems/spiral-matrix/

LeetCode 59. 螺旋矩阵 II

https://leetcode.com/problems/spiral-matrix-ii/
https://leetcode.cn/problems/spiral-matrix-ii/

leetcode 题解 / LeetCode Solutions

https://www.youtube.com/results?search_query=+Leetcode+54

https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE

https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos

类似问题

LeetCode 48. Rotate Image 旋转图像

LCCI 01.07. Rotate Matrix 旋转矩阵

refs

https://leetcode.com/explore/featured/card/top-interview-questions-easy/



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-08-10 23:47  xgqfrms  阅读(18)  评论(2编辑  收藏  举报