ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

leetcode 螺旋矩阵算法题 All In One

2022-08-11 00:02:21  阅读:196  来源: 互联网

标签:right matrix bottom top 矩阵 算法 https leetcode left


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

<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/BJnMZNwUk1M?start=21" title="YouTube video player" width="560"></iframe>

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

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

类似问题

LeetCode 48. Rotate Image 旋转图像

<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/zDQ-O_Vqj0U?start=16" title="YouTube video player" width="560"></iframe>

LCCI 01.07. Rotate Matrix 旋转矩阵

<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/6hXLJF7aOiM?start=16" title="YouTube video player" width="560"></iframe>

refs

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


Flag Counter

©xgqfrms 2012-2020

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

原创文章,版权所有©️xgqfrms, 禁止转载

标签:right,matrix,bottom,top,矩阵,算法,https,leetcode,left
来源: https://www.cnblogs.com/xgqfrms/p/16574373.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有