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

【每日刷题】螺旋矩阵

54. 螺旋矩阵 - 力扣(LeetCode)

先放自己写了好久的模拟代码,自己都被 i 和 j 搞晕了T_T最后还要特判,如果行和列的最小值为奇数,特殊处理一下最中心的数字。更详细的解释放在下面官方题解的代码上。这里对应的是官方题解的方法二“按层遍历”。

class Solution {public List<Integer> spiralOrder(int[][] matrix) {List<Integer> res = new ArrayList<>();int m = matrix.length, n = matrix[0].length;int turn = Math.min(m, n) / 2;for(int i = 0; i < turn; i++) { //需要转圈的次数for(int j = i; j < n - i; j++) { //遍历上res.add(matrix[i][j]);}for(int j = i + 1; j < m - i; j++) { //遍历右res.add(matrix[j][n-1-i]);}for(int j = n - 2 -i; j >= i; j--) { //遍历下res.add(matrix[m-1-i][j]);}for(int j = m - 2 - i; j > i; j--) { //遍历左res.add(matrix[j][i]);}}if(turn * 2 != m && turn * 2 != n) {if(m > n) {for(int i = turn; i < m - turn; i++){res.add(matrix[i][n/2]);}} else {for(int i = turn; i < n - turn; i++){res.add(matrix[m/2][i]);}}}return res;}
}

看完自己的一大堆,再看官方的长代码都觉得眉清目秀了呢【bushi】其实两份代码思路是一样的,将矩阵看成若干层,先输出最外面一圈,接着继续输出第二圈,直到遍历结束。

依据题意,对于每层,从左上方开始顺时针遍历所有元素。当前层的左上角的坐标设为 (top, left),右下角坐标设为 (bottom, right)。角落处的数字在哪一侧遍历都可以,只要保证不会重复遍历即可(这里我跟官方题解稍微有一点出入)。

  • 遍历上侧,(top, left) 到 (top, right)
  • 遍历右侧,(top + 1, right) 到 (bottom, right)
  • 遍历下侧,(bottom, right - 1) 到 (bottom, left)
  • 遍历左侧,(bottom - 1, left ) 到 (top + 1, left)

遍历完当前层的元素之后,将 left 和 top 分别增加 1,将 right 和 bottom 分别减少 1,进入下一层继续遍历,直到遍历完所有元素为止。需要额外注意最中心的那层,如果是单行或者单列,会重复遍历。所以在遍历下侧和遍历左侧时,需要前提条件:如果 left < right 且 top < bottom 才遍历。

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

文章转载自:
http://actinium.dxwdwl.cn
http://anthropolatric.dxwdwl.cn
http://bloodstock.dxwdwl.cn
http://brevirostrate.dxwdwl.cn
http://catercorner.dxwdwl.cn
http://acutilingual.dxwdwl.cn
http://caenozoic.dxwdwl.cn
http://austenian.dxwdwl.cn
http://brythonic.dxwdwl.cn
http://alecost.dxwdwl.cn
http://cabotin.dxwdwl.cn
http://chiffonier.dxwdwl.cn
http://bimotored.dxwdwl.cn
http://cathy.dxwdwl.cn
http://christless.dxwdwl.cn
http://athymic.dxwdwl.cn
http://beltline.dxwdwl.cn
http://canniness.dxwdwl.cn
http://appassionata.dxwdwl.cn
http://callipygian.dxwdwl.cn
http://acinacifoliate.dxwdwl.cn
http://carve.dxwdwl.cn
http://ballplayer.dxwdwl.cn
http://castled.dxwdwl.cn
http://celioscope.dxwdwl.cn
http://ammonium.dxwdwl.cn
http://caffein.dxwdwl.cn
http://archon.dxwdwl.cn
http://cherubic.dxwdwl.cn
http://amulet.dxwdwl.cn
http://www.dtcms.com/a/281584.html

相关文章:

  • 【Python】定时器快速实现
  • 并发编程-volatile
  • Python学习之路(十二)-开发和优化处理大数据量接口
  • git基础命令
  • Redis学习系列之——Redis Stack 拓展功能
  • 为什么市场上电池供电的LoRa DTU比较少?
  • redisson tryLock
  • React源码5 三大核心模块之一:render,renderRoot
  • MMYSQL刷题
  • 北京-4年功能测试2年空窗-报培训班学测开-第五十一天
  • Typecho插件开发:优化文章摘要处理短代码问题
  • 【跟我学YOLO】(2)YOLO12 环境配置与基本应用
  • PID(进程标识符,Process Identifier)是什么?
  • Markdown编辑器--editor.md的用法
  • GTSuite许可管理
  • 学习日志10 python
  • 【鲲苍提效】全面洞察用户体验,助力打造高性能前端应用
  • JAVA青企码协会模式系统源码支持微信公众号+微信小程序+H5+APP
  • vlan作业
  • CommunityToolkit.Mvvm IOC 示例
  • 【Java】JUC并发(线程的方法、多线程的同步并发)
  • 定时器更新中断与串口中断
  • ArrayList列表解析
  • GCC属性修饰符__attribute__((unused))用途
  • 2025国自然青基、面上资助率,或创新低!
  • IPSec和HTTPS对比(一)
  • Java使用itextpdf7生成pdf文档
  • GAMES101 lec1-计算机图形学概述
  • 前端-CSS-day4
  • 边缘计算中模型精度与推理速度的平衡策略及硬件选型