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

LeetCode54题:螺旋矩阵(原创)

【题目描述】

给你一个 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

题目链接. - 力扣(LeetCode)

【解题代码】

package array.matrix;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class SpiralOrder {public static void main(String[] args) {int[][] matrix = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};//int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};//int[][] matrix = new int[][]{{3}, {2}};List<Integer> result = new SpiralOrder().spiralOrder(matrix);System.out.println(Arrays.toString(result.toArray()));}public List<Integer> spiralOrder(int[][] matrix) {List<Integer> result = new ArrayList<>();int direction = 0;int x = -1, y = 0;int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1;while (true) {if (direction == 0) {if (x >= right)break;result.add(matrix[y][++x]);if (x == right) {top++;direction = 1;}} else if (direction == 1) {if (y >= bottom)break;result.add(matrix[++y][x]);if (y == bottom) {right--;direction = 2;}} else if (direction == 2) {if (x <= left)break;result.add(matrix[y][--x]);if (x == left) {bottom--;direction = 3;}} else if (direction == 3) {if (y <= top)break;result.add(matrix[--y][x]);if (y == top) {left++;direction = 0;}}}return result;}
}

【解题思路】

根据题目描述思考,所谓顺时针螺旋循序就是以右->下->左->上的方式四个方向循环缩小的方式访问矩阵数据,一直到当前方向走不通为止。循环好理解,而所谓“缩小访问”就是:右方向访问完了,顶部去掉一层,下方向访问完了,右侧去掉一层,左方向访问完了,底部去掉一层,上方向访问完了,左侧去掉一层。把握好这一点,代码逻辑就好实现了,按照这个思路,很快完成代码编写,并提交成功

【解题步骤】

  1. 定义变量结果集result,访问方向direction,当前访问位置x,y,矩阵上下左右四个边缘值;
    List<Integer> result = new ArrayList<>();
    int direction = 0;
    int x = -1, y = 0;
    int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1;
  2. 外面包一层无限循环来螺旋循序访问矩阵数据
    while (true) {...
    }
  3. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值加入结果集中,如果已经走到最右侧,那么方向改为向下,并将top值加1;
    if (direction == 0) {if (x >= right)break;result.add(matrix[y][++x]);if (x == right) {top++;direction = 1;}
    }
  4. 尝试向下方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置下侧值加入结果集中,如果已经走到最下侧,那么方向改为向左,并将right值减1;
    else if (direction == 1) {if (y >= bottom)break;result.add(matrix[++y][x]);if (y == bottom) {right--;direction = 2;}
    } 
  5. 尝试向左方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置左侧值加入结果集中,如果已经走到最左侧,那么方向改为向上,并将bottom值减1;
    else if (direction == 2) {if (x <= left)break;result.add(matrix[y][--x]);if (x == left) {bottom--;direction = 3;}
    } 
  6. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值,如果已经走到最右侧,那么方向改为向下,并将left值加1;
    else if (direction == 3) {if (y <= top)break;result.add(matrix[--y][x]);if (y == top) {left++;direction = 0;}
    }
  7. 返回结果result
    return result;

【思考总结】

  1. 此题关键点在于顺时针螺旋循序就是以右->下->左->上的方式四个方向循环缩小的方式访问矩阵数据,一直到当前方向走不通为止;
  2. 所谓“缩小访问”就是:右方向访问完了,顶部去掉一层,下方向访问完了,右侧去掉一层,左方向访问完了,底部去掉一层,上方向访问完了,左侧去掉一层。
  3. LeetCode解题之前,一定不要看题解,看了就“破功”了!
http://www.lryc.cn/news/419536.html

相关文章:

  • FPGA常见型号
  • 【多模态大模型】FlashAttention in NeurIPS 2022
  • 过滤器doFilter 方法
  • WPF篇(9)-CheckBox复选框+RadioButton单选框+RepeatButton重复按钮
  • 【机器学习基础】线性回归
  • java基础概念12-二维数组
  • 56 锐键交换机开局
  • VR虚拟展厅与传统实体展厅相比,有哪些优势?
  • Vue的事件处理、事件修饰符、键盘事件
  • c++单例实践
  • SQL注入实例(sqli-labs/less-9)
  • http不同类型方法的作用,get和post区别
  • # 利刃出鞘_Tomcat 核心原理解析(二)
  • 美团秋招笔试第三题(剪彩带)求助帖
  • LeetCode 算法:最小栈 c++
  • 【解压既玩】PS3模拟器v0.0.32+战神3+战神升天+各存档 整合包 ,完美不死机,没有BUG,旷世神作,强力推荐
  • bootstrap- X-editable 行内编辑
  • 【LabVIEW学习篇 - 12】:通知器
  • Oracle一对多(一主多备)的DG环境如何进行switchover切换?
  • 【浏览器插件】Chrome扩展V3版本
  • 编码器信号干扰问题、编码器选型
  • Unity入门5——材质
  • C的温故而知新:存储类别、链接和内存管理(C Primer Plus第十二章)
  • SpringBoot统一功能处理——统一数据返回格式
  • Milvus 实践(2) --- 2.4.x 安装,脚本分析,数据存储解析
  • 【蛋疼c++】千万别用std::wifstream读取Unicode UTF16文件
  • [算法] 第二集 二叉树中的深度搜索
  • 放弃使用外键时,sequelize 应该怎么使用?
  • Microsoft GraphRAG 输出的配置信息
  • 怎么判断张量的维度(形状(shape)),即如何定义行数、列数和深度的?