当前位置: 首页 > news >正文

LeetCodeHot100---螺旋矩阵---一题两解

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

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

方法1:模拟(时间 O(mn),击败100%)

class Solution {public List<Integer> spiralOrder(int[][] matrix) {List<Integer> ans = new ArrayList<>();int m = matrix.length, n = matrix[0].length;int[][] directions = {{0,1},{1,0},{0,-1},{-1,0}};boolean[][] visited = new boolean[m][n];int totalCount = m * n;int row=0, col=0, direction=0;for(int i=0;i<totalCount;i++){ans.add(matrix[row][col]);visited[row][col] = true;int nextRow=row+directions[direction][0];int nextCol=col+directions[direction][1];if(nextRow<0 ||nextRow>=m ||nextCol<0|| nextCol>=n || visited[nextRow][nextCol]){direction=(direction+1)%4;}row=row+directions[direction][0];col=col+directions[direction][1];}return ans; }
}

        模拟方法,就是从 matrix[0][0] 开始,一步一步模拟走的方向。

        既然是模拟走的方向,我们需要解决两个问题:1、怎么让他走?2、要越过边界的时候怎么阻止?

        对于问题1,我们可以在走的时候,将当前位置看作 matrix[row][col],当他要往右走并且不会越界的时候,row 不变,col+1,当他要往下走并不越界的时候,row+1,col 不变,我们可以通过一个二维数组来完成。

        走路过程是:右——下——左——上——右 这样循环,那么二维数组就可以设置成      int[][] directions = {{0,1}, {1,0}, {0.-1}, {-1,0}},这样取 directions[0],然后加到 row 和 col 上,就可以出现 row 不变,col+1 的情况,也就是向右走,然后第二个就是向下走,之后就可以通过 direction = (direction + 1) % 4,获得当前要往哪走。

        知道往哪走,我们要确保不越界的两种情况:1、row < 0, row >= m, col < 0, col >= n;2、当前位置没有被访问过。

        情况1直接写在 if 循环就好,情况2我们可以采用一个二维的 boolean 数组 visited,当访问过之后,visited[row][col] = true,然后 if 循环再判断当前位置是否为 true 就好了。

方法2:逐层模拟:

class Solution {public List<Integer> spiralOrder(int[][] matrix) {int m = matrix.length, n = matrix[0].length;List<Integer> ans = new ArrayList<Integer>();int top=0, bottom=m-1, left=0, right=n-1;while(top<=bottom && left<=right){for(int i=left;i<=right;i++){ans.add(matrix[top][i]);}top++;for(int i=top;i<=bottom;i++){ans.add(matrix[i][right]);}right--;if(top<=bottom && left<=right){for(int i=right;i>=left;i--){ans.add(matrix[bottom][i]);}}bottom--;if(top<=bottom && left<=right){for(int i=bottom;i>=top;i--){ans.add(matrix[i][left]);}}left++;}return ans;}
}

        一层一层往里模拟,最后得到答案。

        左上 matrix[top][left],右上 matrix[top][right],

        左下 matrix[bottom][left],右下 matrix[bottom][right].

http://www.dtcms.com/a/286488.html

相关文章:

  • Unity 常见数据结构分析与实战展示 C#
  • 基于单片机自行车自动防盗报警系统设计
  • VUE目录结构详解
  • NW983NW988美光固态闪存NW991NW992
  • 无符号乘法运算的硬件逻辑实现 ————取自《湖科大教书匠》
  • PAT 1012 The Best Rank
  • QML vscode语法高亮和颜色区分。
  • 【vLLM 学习】Encoder Decoder Multimodal
  • Kotlin集合过滤
  • 有效的括号数据结构oj题(力口20)
  • 无人机传感器模组运行与技术难点分析
  • Axure RP 10 预览显示“无标题文档”的空白问题探索【护航版】
  • 美团闪购最新版 mtgsig1.2
  • LP-MSPM0G3507学习--04GPIO控制
  • 消息队列 2.RabbitMQ
  • Elasticsearch:ES|QL 改进的时间线
  • [3-02-01].第01章:框架概述 - Spring生态
  • 表单、表格字段,输入完毕后立即点击【保存】,导致数据未更新就被保存
  • 【教程】基于无人机的大豆光合效率研究
  • 赛思SLIC芯片、语音芯片原厂 赛思SLIC语音芯片ASX630:国产强“芯”赋能FTTR全光网络​
  • vscode 一直连不上远程,网络是通的,ssh 也能直接登录远程
  • 【科研绘图系列】R语言绘制分组箱线图
  • SDC Specical check setting的描述 - false path
  • Docker笔记-部署Redis集群
  • leetcode15.三数之和题解:逻辑清晰带你分析
  • AWS(基础)
  • 网络基础10 :ACL真机实验
  • Redis原理之哨兵机制(Sentinel)
  • 【洛谷P1417】烹调方案 题解
  • ONNX模型使用指南:从零开始掌握跨领域模型部署