54:Spiral Matrix 螺旋矩阵

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

Example 1:

  1. Input:
  2. [
  3. [ 1, 2, 3 ],
  4. [ 4, 5, 6 ],
  5. [ 7, 8, 9 ]
  6. ]
  7. Output: [1,2,3,6,9,8,7,4,5]

Example 2:

  1. Input:
  2. [
  3. [1, 2, 3, 4],
  4. [5, 6, 7, 8],
  5. [9,10,11,12]
  6. ]
  7. Output: [1,2,3,4,8,12,11,10,9,5,6,7]

解题思路:

​ 参考例二,观察索引改变方式:(0,0)--->(0,3)、(0,3)--->((2,3)--->(2,0)--->(1,0)--->(1,2)

​ 从(0,3)看,分别是:向下 横坐标自增1,到2;向左:纵坐标自减1 ,到0;向上横坐标自减1,到1;向右纵坐标自增1,到2

​ 假如m*n的矩阵,从(0,m-1)开始,向下移动n-1次到达最下面,再向左m-1次,向上n-2次,向右m-2次,接着就是:向下n-3,向左m-3,向上n-4,向右m-4。每次转向m或n都会自减1。

这是我的思路,网上很多都是直接操作索引坐标,我觉得不是很好理解,因为超过一个螺旋的矩阵,每次都要更改参考坐标,不过两种方法本质差别不大

java:

  1. class Solution {
  2. public List<Integer> spiralOrder(int[][] matrix) {
  3. List nums=new ArrayList();
  4. if (matrix.length==0||matrix[0].length==0)return nums ;
  5. int row=matrix.length-1,col=matrix[0].length-1,m=0,n=0,i=-1,tmp=0;
  6. while (row>=0&&col>=0){
  7. switch (i++%4){
  8. case 0:
  9. for (tmp=0;tmp<row;tmp++)
  10. nums.add(matrix[++m][n]);row-=1;
  11. break;
  12. case 1:
  13. for (tmp=0;tmp<col;tmp++)
  14. nums.add(matrix[m][--n]);col-=1;
  15. break;
  16. case 2:
  17. for (tmp=0;tmp<row;tmp++)
  18. nums.add(matrix[--m][n]);row-=1;
  19. break;
  20. case 3:
  21. for (tmp=0;tmp<col;tmp++)
  22. nums.add(matrix[m][++n]);col-=1;
  23. break;
  24. default:
  25. for (tmp=0;tmp<=col;tmp++)
  26. nums.add(matrix[m][n++]);tmp=0;n-=1;
  27. break;
  28. }
  29. }
  30. return nums;
  31. }
  32. }

注意点:

​ 先判断是否为空数组,判断条件顺序不能颠倒。因为如果 matrix.length==0 判断为true,则后面的 matrix[0].length==0 不会再判断,即返回空数组;但是matrix[0].length==0 在前时,如果输入数组为空,matrix[0] 会报错因为matrix并没有0号索引。

python3:

  1. class Solution:
  2. def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
  3. if len(matrix)==0 or len(matrix[0])==0:return []
  4. nums=[];m=0;n=0;row=len(matrix)-1;col=len(matrix[0])-1;flag=0;
  5. for n in range(col+1):nums.append(matrix[m][n])
  6. while row>=0 and col>=0:
  7. if flag % 4 == 0:
  8. for i in range(row):
  9. m+=1
  10. nums.append(matrix[m][n])
  11. row -= 1
  12. elif flag % 4==1:
  13. for i in range(col):
  14. n-=1
  15. nums.append(matrix[m][n])
  16. col -= 1
  17. elif flag % 4 == 2:
  18. for i in range(row):
  19. m-=1
  20. nums.append(matrix[m][n])
  21. row -= 1
  22. elif flag % 4 == 3:
  23. for i in range(col):
  24. n+=1
  25. nums.append(matrix[m][n])
  26. col -= 1
  27. flag+=1
  28. return nums

注意点:

​ python没有switch...case...语句。for循环可操作性很高,可以直接操作索引坐标改变遍历方式,不再赘述。

Leetcode 54:Spiral Matrix 螺旋矩阵的更多相关文章

  1. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  2. [leetcode]54. Spiral Matrix螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  6. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  7. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  8. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

随机推荐

  1. 【FFT初识】

      FFT在用于解决多项式乘法A*B(A和B为多项式,形如a0+a1*x^1+a2*x^2....)的时候,通俗地解释就是: 原理:先根据各自的系数各自转化为对应的向量(O(nlogn)),然后向量相 ...

  2. python名片管理系统V2

    主程序: #! /usr/bin env python3 # -*- coding: utf-8 -*- # 项目三: # 1.要求:编写一个名片管理系统,功能如下: # 用户输入相对应的指令,实现对 ...

  3. bzoj1101

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2319  Solved: 936[Submit][Status] ...

  4. 使用Jquery动态加入对象的集合属性,提交集合属性到表单

    1.设置模型,引入构造函数,初始化集合 public class Person { public Person() //引入构造函数,初始化集合.如果未设置构造函数,集合会出现错误. { Skills ...

  5. 关于使用java执行shell脚本获取centos的硬盘序列号和mac地址

    1.获取硬盘序列号: 新建shell脚本文件: identifier.sh, 内容为: diskdata=`fdisk -l` diskleft=${diskdata#*"identifie ...

  6. Rails 浅谈 ActiveRecord 的 N + 1 查询问题(copy)

    [说明:资料来自https://ruby-china.org/topics/32364] ORM框架的性能小坑 在使用ActiveRecord这样的ORM工具时,常会嵌套遍历model.例如,有两个m ...

  7. 适用于PHP初学者的学习线路和建议

    [导读] 这篇文章是围绕PHP的学习问题,之前介绍过<重磅资料!Github上的PHP资源汇总大全><深入探讨PHP类的封装与继承><PHP的学习规划建议>等对PH ...

  8. 洛谷 P2763 试题库问题【最大流】

    s向所有类别属性连流量为当前类别属性需要的个数的边,所有题目向t连流量为1的边(表示只能选一次),所有属性向含有它的题连容量为1的边.跑一变dinic,结果小于m则无解,否则看每一个类别属性连出去的题 ...

  9. [App Store Connect帮助]八、维护您的 App(4.4)重置 App 总评分(iOS、Apple TVOS、macOS)

    当您发布新版本时,您可以重置 App 评分.您的产品页将显示一则消息,说明 App 的总评分最近已重置.此消息将一直显示,直到有足够多的顾客对新版本进行了评分且页面出现新的总评分. 评分只可以针对全球 ...

  10. RT-Thread 设备驱动ADC浅析与改进

    OS版本:RT-Thread 4.0.0 芯片:STM32F407 下面时官方ADC提供的参考访问接口 访问 ADC 设备 应用程序通过 RT-Thread 提供的 ADC 设备管理接口来访问 ADC ...