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:

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

Example 2:

Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
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:

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List nums=new ArrayList();
if (matrix.length==0||matrix[0].length==0)return nums ;
int row=matrix.length-1,col=matrix[0].length-1,m=0,n=0,i=-1,tmp=0; while (row>=0&&col>=0){
switch (i++%4){
case 0:
for (tmp=0;tmp<row;tmp++)
nums.add(matrix[++m][n]);row-=1;
break;
case 1:
for (tmp=0;tmp<col;tmp++)
nums.add(matrix[m][--n]);col-=1;
break;
case 2:
for (tmp=0;tmp<row;tmp++)
nums.add(matrix[--m][n]);row-=1;
break;
case 3:
for (tmp=0;tmp<col;tmp++)
nums.add(matrix[m][++n]);col-=1;
break;
default:
for (tmp=0;tmp<=col;tmp++)
nums.add(matrix[m][n++]);tmp=0;n-=1;
break;
}
}
return nums;
}
}

注意点:

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

python3:

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if len(matrix)==0 or len(matrix[0])==0:return []
nums=[];m=0;n=0;row=len(matrix)-1;col=len(matrix[0])-1;flag=0;
for n in range(col+1):nums.append(matrix[m][n])
while row>=0 and col>=0:
if flag % 4 == 0:
for i in range(row):
m+=1
nums.append(matrix[m][n])
row -= 1
elif flag % 4==1:
for i in range(col):
n-=1
nums.append(matrix[m][n])
col -= 1
elif flag % 4 == 2:
for i in range(row):
m-=1
nums.append(matrix[m][n])
row -= 1
elif flag % 4 == 3:
for i in range(col):
n+=1
nums.append(matrix[m][n])
col -= 1
flag+=1
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. 【转】使用git将项目上传到github(最简单方法)

    原文地址:http://www.cnblogs.com/cxk1995/p/5800196.html 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ ...

  2. Linq list 排序,Dictionary 排序

    C# 对List成员排序的简单方法 http://blog.csdn.net/wanzhuan2010/article/details/6205884 LINQ之路系列博客导航 http://www. ...

  3. bzoj 1601: [Usaco2008 Oct]灌水【最小生成树】

    挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...

  4. hdu 1071 The area【定积分】

    用顶点式\( a(x-h)^2+k=y \)解方程,转化为\(ax^2+bx+c=y \)的形式,然后对二次函数求定积分\( \frac{ax^3}{3}+\frac{bx^2}{2}+cx+C \) ...

  5. 洛谷 P2762 太空飞行计划问题 【最大权闭合子图+最小割】

    --一道难在读入的题. 最后解决方案直接getline一行然后是把读优拆掉放进函数,虽然很丑但是过了. 然后就是裸的最大权闭合子图了,把仪器当成负权点向t连流量为其价格的边,s向实验连流量为实验报酬的 ...

  6. Tensor Operation

    Main operation categories that encompass the operations of tensors. Reshaping operations Element-wis ...

  7. 关于新手html的认识 以及对table的基本用法

    1.html语言 <!DOCTYPE html> 声明html <!--双标签--> <!--<html 属性名="属性值 " 属性名2=&qu ...

  8. iOS导航栏NavigationBar的颜色,按钮和标题以及字体颜色

    首先,层级关系: leftBarButtonItem.rightBarButtonItem.title都是加在UINavigationItem上的,UINavigationItem再加在Navigat ...

  9. [转]POJ WA/RE指南

    "POJ上头的题都是数学题",也不知道是那个家伙胡诌的--但是POJ的要求就是算法通过了也不让你AC.下面本人就这560题的经验,浅谈一下WA/RE了怎么办.  以下内容是扯淡-- ...

  10. _bzoj1012 [JSOI2008]最大数maxnumber【Fenwick Tree】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 裸的树状数组. #include <cstdio> #include &l ...