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

019螺旋矩阵

螺旋矩阵

题目链接:https://leetcode.cn/problems/spiral-matrix/description/?envType=study-plan-v2&envId=top-100-liked

我的解答:

public List<Integer> spiralOrder(int[][] matrix) { int m=matrix.length, n=matrix[0].length; int up=0, down=m-1, left=0, right=n-1; int i; int cnt=0, count=m*n; List<Integer> ans = new ArrayList<>(); while(cnt<count){ for(i=left; i<=right&&cnt<count; i++){ ans.add(matrix[up][i]); cnt++; } up++; for(i=up; i<=down&&cnt<count; i++){ ans.add(matrix[i][right]); cnt++; } right--; for(i=right; i>=left&&cnt<count; i--){ ans.add(matrix[down][i]); cnt++; } down--; for(i=down; i>=up&&cnt<count; i--){ ans.add(matrix[i][left]); cnt++; } left++; } return ans; }

分析:代码的时间复杂度为O(m*n),空间复杂度为O(1)。解题思路:模拟螺旋矩阵,用up、right、down、left分别控制每次向上、右、下、左移动的边界,每次移动后维护边界值的变化,然后按顺序遍历矩阵即可。

看了官方题解后的解答:

//方法一:模拟 //时间复杂度:O(mn) //空间复杂度:O(mn) public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ans = new ArrayList<>(); if(matrix==null || matrix.length==0 || matrix[0].length==0){ return ans; } int m = matrix.length, n = matrix[0].length; int total = m*n; int[][] directions = new int[][]{{0,1},{1,0},{0,-1},{-1,0}}; boolean[][] flag = new boolean[m][n]; int direction = 0; int row=0, column=0; int newRow, newColumn; for(int i=0; i<total; i++){ ans.add(matrix[row][column]); flag[row][column]=true; newRow = row+directions[direction][0]; newColumn = column+directions[direction][1]; if(newRow<0 || newRow>=m || newColumn<0 || newColumn>=n || flag[newRow][newColumn]){ direction = (direction+1)%4;//一共四个移动方向,下标从0-3 } row = row+directions[direction][0]; column = column+directions[direction][1]; } return ans; } //方法二:按层模拟 //时间复杂度:O(mn) //空间复杂度:O(1) public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ans = new ArrayList<>(); if(matrix==null || matrix.length==0 || matrix[0].length==0){ return ans; } int m = matrix.length, n = matrix[0].length; int top=0, bottom=m-1, left=0, right=n-1; while(left<=right && top<=bottom){ //当前层从左往右遍历 for(int column=left; column<=right; column++){ ans.add(matrix[top][column]); } //当前层从上往下遍历 for(int row=top+1; row<=bottom; row++){ ans.add(matrix[row][right]); } //矩阵还未遍历完成且当前层不仅只剩一行或者仅剩一列 if(left<right && top<bottom){ //当前层从右往左遍历 for(int column=right-1; column>=left; column--){ ans.add(matrix[bottom][column]); } //当前层从下往上遍历 for(int row=bottom-1; row>top; row--){ ans.add(matrix[row][left]); } } top++; bottom--; left++; right--; } return ans; }

分析:

​ 1、方法一的解题思路:用一个二维数组direction维护移动方向,按照向右、向下、向左、向上的顺序,再用一个二维数组flag维护当前位置是否被访问过,每次移动后若超出边界或者位置已经被访问过,则需要跟新移动方向。

​ 2、方法二的解题思路:从最外层往开始遍历,每次遍历一层,遍历完后同时缩小上、下、左、右边界。注意,当矩阵最后只剩一行未遍历时,top等于bottom,所以在进行从右向左和从下往上遍历前需要进行判断,否则会重复遍历;当矩阵最后只剩一列遍历时同理。

总结

  • 本题主要需要模拟按从左往右、从上往下、从右往左、从下往上顺序遍历的过程。
http://www.jsqmd.com/news/763841/

相关文章:

  • 2026力矩传感器推荐排名,广东犸力品质靠谱口碑俱佳 - 品牌速递
  • 哈尔滨铜门厂家严寒适配核心工艺技术全解析 - 资讯焦点
  • 创建自己的obsidian模版
  • 从GoogleTest断言看C++单元测试设计:如何写出像产品代码一样优雅的测试?
  • VLC媒体播放器终极指南:10个技巧让你成为播放大师 [特殊字符]
  • 压缩包密码找回终极指南:3步解锁你的加密文件
  • 从安装到建表:KingbaseES V8数据库新手避坑指南(附常用SQL速查)
  • 别等审计飞检才后悔!VSCode 2026医疗校验工具已内置中国《医疗器械软件注册审查指导原则》第4.2.1条智能判据(仅限首批2000个企业License)
  • 2026压力传感器排行榜,广东犸力跻身头部品牌,实力不容小觑 - 品牌速递
  • 哈尔滨铜门厂家技术解析:严寒适配与定制工艺全拆解 - 资讯焦点
  • 如何用渔人的直感成为FF14钓鱼大师:终极计时器完全指南
  • Docker低代码容器化陷阱曝光:87%团队踩坑的YAML自动生成漏洞及军工级修复方案
  • 【限时开放】VSCode 2026多智能体协同编程认证路径(含微软官方未公布的3个隐藏调试命令+Agent健康度诊断CLI工具)
  • FFXIVChnTextPatch:3分钟为FF14国际服注入完美中文补丁的终极指南
  • 软考 系统架构设计师系列知识点之云原生架构设计理论与实践(26)
  • 油痘肌及油敏痘肌洁面科学评测:无极秀净肤氨基酸洗面乳 控油修护双赋能 - 资讯焦点
  • DDR DFI接口时序详解:搞懂MC与PHY之间那些‘握手’与‘等待’的信号
  • 多任务求解器架构设计与工程优化实践
  • 基于GPT-4与Veo3的AI视频生成:构建24秒故事短片的自动化工作流
  • 2026 年 5 月国内外超声波热量表十大品牌排名 - 仪表人小余
  • 告别命令行:在Ubuntu 22.04桌面为EasyConnect创建稳定可用的启动器图标
  • 终极指南:如何用Harepacker复活版打造你的专属冒险世界
  • 告别文件分享烦恼:彩虹外链网盘如何让你的文件管理变得简单高效
  • 如何快速部署Nettu Meet开源视频会议系统:完整企业级协作平台指南
  • 5分钟掌握Python无人机编程:DroneKit-Python让你的无人机飞起来!
  • 为什么你的Windows触控板总感觉不够顺手?三指拖拽功能让你体验MacBook般的流畅操作!
  • 要求不高却单身,问题到底出在哪?他趣前来答疑解惑 - 资讯焦点
  • MPC与漏斗控制器的工业过程协同控制设计
  • Windows触控板三指拖拽完全指南:如何实现MacBook般的流畅体验
  • Windows终极优化指南:Chris Titus Tech WinUtil完全使用教程